private void writeUpdatedTMX (TiledMap tiledMap, FileHandle tmxFileHandle) throws IOException { Document doc; DocumentBuilder docBuilder; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); try { docBuilder = docFactory.newDocumentBuilder(); doc = docBuilder.parse(tmxFileHandle.read()); Node map = doc.getFirstChild(); while (map.getNodeType() != Node.ELEMENT_NODE || map.getNodeName() != "map") { if ((map = map.getNextSibling()) == null) { throw new GdxRuntimeException("Couldn't find map node!"); } } setProperty(doc, map, "atlas", settings.tilesetOutputDirectory + "/" + settings.atlasOutputName + ".atlas"); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); outputDir.mkdirs(); StreamResult result = new StreamResult(new File(outputDir, tmxFileHandle.name())); transformer.transform(source, result); } catch (ParserConfigurationException e) { throw new RuntimeException("ParserConfigurationException: " + e.getMessage()); } catch (SAXException e) { throw new RuntimeException("SAXException: " + e.getMessage()); } catch (TransformerConfigurationException e) { throw new RuntimeException("TransformerConfigurationException: " + e.getMessage()); } catch (TransformerException e) { throw new RuntimeException("TransformerException: " + e.getMessage()); } }
private void writeUpdatedTMX (TiledMap tiledMap, FileHandle tmxFileHandle) throws IOException { Document doc; DocumentBuilder docBuilder; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); try { docBuilder = docFactory.newDocumentBuilder(); doc = docBuilder.parse(tmxFileHandle.read()); Node map = doc.getFirstChild(); while (map.getNodeType() != Node.ELEMENT_NODE || map.getNodeName() != "map") { if ((map = map.getNextSibling()) == null) { throw new GdxRuntimeException("Couldn't find map node!"); } } setProperty(doc, map, "atlas", settings.tilesetOutputDirectory + "/" + settings.atlasOutputName + ".atlas"); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); outputDir.mkdirs(); StreamResult result = new StreamResult(new File(outputDir, tmxFileHandle.name())); transformer.transform(source, result); } catch (ParserConfigurationException e) { throw new RuntimeException("ParserConfigurationException: " + e.getMessage()); } catch (SAXException e) { throw new RuntimeException("SAXException: " + e.getMessage()); } catch (TransformerConfigurationException e) { throw new RuntimeException("TransformerConfigurationException: " + e.getMessage()); } catch (TransformerException e) { throw new RuntimeException("TransformerException: " + e.getMessage()); } }
/** * Replaces package value with passed packageOriginal string * * @param file File for AndroidManifest.xml * @param packageOriginal Package name to replace * @throws AndrolibException */ public static void renameManifestPackage(File file, String packageOriginal) throws AndrolibException { try { Document doc = loadDocument(file); // Get the manifest line Node manifest = doc.getFirstChild(); // update package attribute NamedNodeMap attr = manifest.getAttributes(); Node nodeAttr = attr.getNamedItem("package"); nodeAttr.setNodeValue(packageOriginal); saveDocument(file, doc); } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { } }
String xml = "<add job=\"351\">\n" + " <tag>foobar</tag>\n" + " <tag>foobar2</tag>\n" + "</add>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes()); Document doc = db.parse(bis); Node n = doc.getFirstChild(); NodeList nl = n.getChildNodes(); Node an,an2; for (int i=0; i < nl.getLength(); i++) { an = nl.item(i); if(an.getNodeType()==Node.ELEMENT_NODE) { NodeList nl2 = an.getChildNodes(); for(int i2=0; i2<nl2.getLength(); i2++) { an2 = nl2.item(i2); // DEBUG PRINTS System.out.println(an2.getNodeName() + ": type (" + an2.getNodeType() + "):"); if(an2.hasChildNodes()) System.out.println(an2.getFirstChild().getTextContent()); if(an2.hasChildNodes()) System.out.println(an2.getFirstChild().getNodeValue()); System.out.println(an2.getTextContent()); System.out.println(an2.getNodeValue()); } } }
/** * Removes attributes like "versionCode" and "versionName" from file. * * @param file File representing AndroidManifest.xml * @throws AndrolibException */ public static void removeManifestVersions(File file) throws AndrolibException { if (file.exists()) { try { Document doc = loadDocument(file); Node manifest = doc.getFirstChild(); NamedNodeMap attr = manifest.getAttributes(); Node vCode = attr.getNamedItem("android:versionCode"); Node vName = attr.getNamedItem("android:versionName"); if (vCode != null) { attr.removeNamedItem("android:versionCode"); } if (vName != null) { attr.removeNamedItem("android:versionName"); } saveDocument(file, doc); } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { } } }
private void parseXml(final Reader in) throws SAXException, IOException, ParserConfigurationException { final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(in)); assertNotNull(doc); assertEquals("X", doc.getFirstChild().getNodeName()); }
private void parseXml(final InputStream in) throws SAXException, IOException, ParserConfigurationException { final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(in)); assertNotNull(doc); assertEquals("X", doc.getFirstChild().getNodeName()); }
private Node getNode( JobEntryJob jej ) throws Exception { String string = "<job>" + jej.getXML() + "</job>"; InputStream stream = new ByteArrayInputStream( string.getBytes( StandardCharsets.UTF_8 ) ); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return db.parse( stream ).getFirstChild(); }
/** * Build <code>cache</code> element map for given <cod>doc</code>'s schemaLocation for * {@link CacheXml#GEODE_NAMESPACE}. * * @param doc {@link Document} to parse schema for. * @return Element map * @since GemFire 8.1 */ public static LinkedHashMap<String, CacheElement> buildElementMap(final Document doc) throws IOException, XPathExpressionException, SAXException, ParserConfigurationException { Node cacheNode = doc.getFirstChild(); if ("#comment".equals(cacheNode.getNodeName())) { cacheNode = cacheNode.getNextSibling(); } final Map<String, String> schemaLocationMap = XmlUtils.buildSchemaLocationMap(getAttribute(cacheNode, W3C_XML_SCHEMA_INSTANCE_ATTRIBUTE_SCHEMA_LOCATION, W3C_XML_SCHEMA_INSTANCE_NS_URI)); final LinkedHashMap<String, CacheElement> elementMap = new LinkedHashMap<>(); buildElementMapCacheType(elementMap, resolveSchema(schemaLocationMap, CacheXml.GEODE_NAMESPACE)); // if we are ever concerned with the order of extensions or children process them here. return elementMap; }
private static String getJavaDocText(DetailAST node) { final String text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<document>\n" + node.getFirstChild().getText().replaceAll("(^|\\r?\\n)\\s*\\* ?", "\n") .trim() + "\n</document>"; String result = null; try { result = getNodeText(XmlUtil.getRawXml(checkName, text, text).getFirstChild(), false).replace("\r", ""); } catch (ParserConfigurationException ex) { Assert.fail("Exception: " + ex.getClass() + " - " + ex.getMessage()); } return result; }
public void executeWorkItem(WorkItem workItem, WorkItemManager mgr) { try { Document document = DocumentBuilderFactory .newInstance() .newDocumentBuilder() .parse(new ByteArrayInputStream( "<user hello='hello world' />" .getBytes())); Map<String, Object> params = new HashMap<String, Object>(); params.put("output", document.getFirstChild()); mgr.completeWorkItem(workItem.getId(), params); } catch (Throwable e) { throw new RuntimeException(e); } }
public void executeWorkItem(WorkItem workItem, WorkItemManager mgr) { try { Document document = DocumentBuilderFactory .newInstance() .newDocumentBuilder() .parse(new ByteArrayInputStream( "<user hello='hello world' />" .getBytes())); Map<String, Object> params = new HashMap<String, Object>(); params.put("output", document.getFirstChild()); mgr.completeWorkItem(workItem.getId(), params); } catch (Throwable e) { throw new RuntimeException(e); } }
@Test public void testLoadXML() throws Exception { LoadFileInputMeta origMeta = createMeta(); LoadFileInputMeta testMeta = new LoadFileInputMeta(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse( new InputSource( new StringReader( "<step>" + xmlOrig + "</step>" ) ) ); IMetaStore metaStore = null; testMeta.loadXML( doc.getFirstChild(), null, metaStore ); assertEquals( origMeta, testMeta ); }
public Node getEntryNode( String variable_name, String variable_value ) throws ParserConfigurationException, SAXException, IOException { StringBuilder sb = new StringBuilder(); sb.append( XMLHandler.openTag( "job" ) ); sb.append( " " ).append( XMLHandler.openTag( "fields" ) ); sb.append( " " ).append( XMLHandler.openTag( "field" ) ); sb.append( " " ).append( XMLHandler.addTagValue( "variable_name", variable_name ) ); if ( variable_value != null ) { sb.append( " " ).append( XMLHandler.addTagValue( "variable_value", variable_value ) ); } sb.append( " " ).append( XMLHandler.closeTag( "field" ) ); sb.append( " " ).append( XMLHandler.closeTag( "fields" ) ); sb.append( XMLHandler.closeTag( "job" ) ); InputStream stream = new ByteArrayInputStream( sb.toString().getBytes( StandardCharsets.UTF_8 ) ); DocumentBuilder db; Document doc; db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = db.parse( stream ); Node entryNode = doc.getFirstChild(); return entryNode; }
@Test public void testEquals() throws IOException, KettleXMLException { String functionXml = IOUtils.toString( UnivariateStatsMetaTest.class.getClassLoader().getResourceAsStream( "org/pentaho/di/trans/steps/univariatestats/trueValuesUnivariateStatsMetaFunctionNode.xml" ) ); UnivariateStatsMetaFunction function = new UnivariateStatsMetaFunction( XMLHandler.loadXMLString( functionXml ).getFirstChild() ); UnivariateStatsMetaFunction function2 = new UnivariateStatsMetaFunction( XMLHandler.loadXMLString( functionXml ).getFirstChild() ); assertEquals( function, function2 ); functionXml = IOUtils.toString( UnivariateStatsMetaTest.class.getClassLoader().getResourceAsStream( "org/pentaho/di/trans/steps/univariatestats/falseValuesUnivariateStatsMetaFunctionNode.xml" ) ); function = new UnivariateStatsMetaFunction( XMLHandler.loadXMLString( functionXml ).getFirstChild() ); assertFalse( function.equals( function2 ) ); function2 = new UnivariateStatsMetaFunction( XMLHandler.loadXMLString( functionXml ).getFirstChild() ); assertEquals( function, function2 ); }
public Node getEntryNode( boolean includeTransname, ObjectLocationSpecificationMethod method ) throws ParserConfigurationException, SAXException, IOException { JobEntryTrans jobEntryTrans = getJobEntryTrans(); jobEntryTrans.setDescription( JOB_ENTRY_DESCRIPTION ); jobEntryTrans.setFileName( JOB_ENTRY_FILE_NAME ); jobEntryTrans.setDirectory( JOB_ENTRY_FILE_DIRECTORY ); if ( includeTransname ) { jobEntryTrans.setTransname( JOB_ENTRY_FILE_NAME ); } if ( method != null ) { jobEntryTrans.setSpecificationMethod( method ); } String string = "<job>" + jobEntryTrans.getXML() + "</job>"; InputStream stream = new ByteArrayInputStream( string.getBytes( StandardCharsets.UTF_8 ) ); DocumentBuilder db; Document doc; db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = db.parse( stream ); Node entryNode = doc.getFirstChild(); return entryNode; }
/** * Asserts that Cache type is defined as an embedded ComplexType under an Element named "cache" * latest version. If this assertion fails it likely means that cache type is defined as a named * ComplexType or reference. Update code in CacheElement.buildElementMapCacheType to account for * this. * * @since GemFire 8.1 */ @Test public void testCacheDefinedAsNestedComplexTypeLatest() throws Exception { Document doc = loadSchema(CacheXml.LATEST_SCHEMA_LOCATION); final XPathContext xPathContext = new XPathContext(CacheElement.XSD_PREFIX, XMLConstants.W3C_XML_SCHEMA_NS_URI); final Node cacheType = XmlUtils.querySingleElement(doc.getFirstChild(), CacheElement.CACHE_TYPE_EMBEDDED, xPathContext); assertNotNull("Cache type is not embedded complexType.", cacheType); }
/** * Asserts that Cache type is defined as a embedded ComplexType under an Element named "cache" for * version 8.1. * * @since GemFire 8.1 */ @Test public void testCacheDefinedAsNestedComplexType8_1() throws Exception { Document doc = loadSchema(CacheXmlVersion.GEMFIRE_8_1.getSchemaLocation()); final XPathContext xPathContext = new XPathContext(CacheElement.XSD_PREFIX, XMLConstants.W3C_XML_SCHEMA_NS_URI); final Node cacheType = XmlUtils.querySingleElement(doc.getFirstChild(), CacheElement.CACHE_TYPE_EMBEDDED, xPathContext); assertNotNull("Cache type is not embedded complexType.", cacheType); }
@Test public void testNodeConstructor() throws IOException, KettleXMLException { String functionXml = IOUtils.toString( UnivariateStatsMetaTest.class.getClassLoader().getResourceAsStream( "org/pentaho/di/trans/steps/univariatestats/trueValuesUnivariateStatsMetaFunctionNode.xml" ) ); UnivariateStatsMetaFunction function = new UnivariateStatsMetaFunction( XMLHandler.loadXMLString( functionXml ).getFirstChild() ); assertEquals( "a", function.getSourceFieldName() ); assertTrue( function.getCalcN() ); assertTrue( function.getCalcMean() ); assertTrue( function.getCalcStdDev() ); assertTrue( function.getCalcMin() ); assertTrue( function.getCalcMax() ); assertTrue( function.getCalcMedian() ); assertEquals( 0.5, function.getCalcPercentile(), 0 ); assertTrue( function.getInterpolatePercentile() ); functionXml = IOUtils.toString( UnivariateStatsMetaTest.class.getClassLoader().getResourceAsStream( "org/pentaho/di/trans/steps/univariatestats/falseValuesUnivariateStatsMetaFunctionNode.xml" ) ); function = new UnivariateStatsMetaFunction( XMLHandler.loadXMLString( functionXml ).getFirstChild() ); assertTrue( Utils.isEmpty( function.getSourceFieldName() ) ); assertFalse( function.getCalcN() ); assertFalse( function.getCalcMean() ); assertFalse( function.getCalcStdDev() ); assertFalse( function.getCalcMin() ); assertFalse( function.getCalcMax() ); assertFalse( function.getCalcMedian() ); assertEquals( -1.0, function.getCalcPercentile(), 0 ); assertFalse( function.getInterpolatePercentile() ); }
@Test public void testLegacyLoadXml() throws IOException, KettleXMLException { String legacyXml = IOUtils.toString( UnivariateStatsMetaTest.class.getClassLoader().getResourceAsStream( "org/pentaho/di/trans/steps/univariatestats/legacyUnivariateStatsMetaTest.xml" ) ); IMetaStore mockMetaStore = mock( IMetaStore.class ); UnivariateStatsMeta meta = new UnivariateStatsMeta(); meta.loadXML( XMLHandler.loadXMLString( legacyXml ).getFirstChild(), new ArrayList<DatabaseMeta>(), mockMetaStore ); assertEquals( 2, meta.getNumFieldsToProcess() ); UnivariateStatsMetaFunction first = meta.getInputFieldMetaFunctions()[0]; assertEquals( "a", first.getSourceFieldName() ); assertEquals( true, first.getCalcN() ); assertEquals( true, first.getCalcMean() ); assertEquals( true, first.getCalcStdDev() ); assertEquals( true, first.getCalcMin() ); assertEquals( true, first.getCalcMax() ); assertEquals( true, first.getCalcMedian() ); assertEquals( .5, first.getCalcPercentile(), 0 ); assertEquals( true, first.getInterpolatePercentile() ); UnivariateStatsMetaFunction second = meta.getInputFieldMetaFunctions()[1]; assertEquals( "b", second.getSourceFieldName() ); assertEquals( false, second.getCalcN() ); assertEquals( false, second.getCalcMean() ); assertEquals( false, second.getCalcStdDev() ); assertEquals( false, second.getCalcMin() ); assertEquals( false, second.getCalcMax() ); assertEquals( false, second.getCalcMedian() ); assertEquals( -1.0, second.getCalcPercentile(), 0 ); assertEquals( false, second.getInterpolatePercentile() ); }