com.helger.xml.serialize.write.XMLWriterSettings Java Examples
The following examples show how to use
com.helger.xml.serialize.write.XMLWriterSettings.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PSWriterTest.java From ph-schematron with Apache License 2.0 | 6 votes |
@Test public void testWriteWithNamespacePrefix () throws SchematronReadException { final IReadableResource aRes = SchematronTestHelper.getAllValidSchematronFiles ().getFirst (); // Read existing Schematron final PSSchema aSchema = new PSReader (aRes).readSchema (); // Create the XML namespace context final MapBasedNamespaceContext aNSCtx = PSWriterSettings.createNamespaceMapping (aSchema); aNSCtx.removeMapping (XMLConstants.DEFAULT_NS_PREFIX); aNSCtx.addMapping ("sch", CSchematron.NAMESPACE_SCHEMATRON); // Create the PSWriter settings final PSWriterSettings aPSWS = new PSWriterSettings (); aPSWS.setXMLWriterSettings (new XMLWriterSettings ().setNamespaceContext (aNSCtx) .setPutNamespaceContextPrefixesInRoot (true)); // Write the Schematron new PSWriter (aPSWS).writeToFile (aSchema, new File ("target/test-with-nsprefix.xml")); }
Example #2
Source File: MicroWriterTest.java From ph-commons with Apache License 2.0 | 6 votes |
private static void _testGetNodeAsXMLString (final IMicroNode aNode) { // try all permutations final XMLWriterSettings aSettings = new XMLWriterSettings (); for (int nCharSet = 0; nCharSet < 2; ++nCharSet) { aSettings.setCharset (nCharSet == 1 ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8); for (final EXMLSerializeIndent eIndent : EXMLSerializeIndent.values ()) { aSettings.setIndent (eIndent); for (final EXMLSerializeDocType eDocType : EXMLSerializeDocType.values ()) { aSettings.setSerializeDocType (eDocType); assertNotNull (MicroWriter.getNodeAsString (aNode, aSettings)); } } } }
Example #3
Source File: MicroWriterTest.java From ph-commons with Apache License 2.0 | 6 votes |
private static void _testGetNodeAsXHTMLString (final IMicroNode aNode) { // try all permutations final XMLWriterSettings aSettings = XMLWriterSettings.createForXHTML (); for (int nCharSet = 0; nCharSet < 2; ++nCharSet) { aSettings.setCharset (nCharSet == 1 ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8); for (final EXMLSerializeIndent eIndent : EXMLSerializeIndent.values ()) { aSettings.setIndent (eIndent); for (final EXMLSerializeDocType eDocType : EXMLSerializeDocType.values ()) { aSettings.setSerializeDocType (eDocType); assertNotNull (MicroWriter.getNodeAsString (aNode, aSettings)); } } } }
Example #4
Source File: MicroReaderTest.java From ph-commons with Apache License 2.0 | 6 votes |
@Test public void testSpecialXMLAttrs () { final String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<root xml:lang=\"en\" xml:space=\"preserve\" xml:base=\"baseuri\" xml:id=\"4711\">" + "Bla" + "</root>"; final IMicroDocument aDoc = MicroReader.readMicroXML (s); assertNotNull (aDoc); final IMicroElement eRoot = aDoc.getDocumentElement (); assertEquals ("en", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "lang")); assertNull (eRoot.getAttributeValue ("lang")); assertEquals ("preserve", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "space")); assertEquals ("baseuri", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "base")); assertEquals ("4711", eRoot.getAttributeValue (XMLConstants.XML_NS_URI, "id")); // Ensure they are written as well assertEquals (s, MicroWriter.getNodeAsString (aDoc, new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE))); }
Example #5
Source File: XMLListHandler.java From ph-commons with Apache License 2.0 | 6 votes |
/** * Write the passed collection to the passed output stream using the * predefined XML layout. * * @param aCollection * The map to be written. May not be <code>null</code>. * @param aOS * The output stream to write to. The stream is closed independent of * success or failure. May not be <code>null</code>. * @return {@link ESuccess#SUCCESS} when everything went well, * {@link ESuccess#FAILURE} otherwise. */ @Nonnull public static ESuccess writeList (@Nonnull final Collection <String> aCollection, @Nonnull @WillClose final OutputStream aOS) { ValueEnforcer.notNull (aCollection, "Collection"); ValueEnforcer.notNull (aOS, "OutputStream"); try { final IMicroDocument aDoc = createListDocument (aCollection); return MicroWriter.writeToStream (aDoc, aOS, XMLWriterSettings.DEFAULT_XML_SETTINGS); } finally { StreamHelper.close (aOS); } }
Example #6
Source File: XMLMapHandler.java From ph-commons with Apache License 2.0 | 6 votes |
/** * Write the passed map to the passed output stream using the predefined XML * layout. * * @param aMap * The map to be written. May not be <code>null</code>. * @param aOS * The output stream to write to. The stream is closed independent of * success or failure. May not be <code>null</code>. * @return {@link ESuccess#SUCCESS} when everything went well, * {@link ESuccess#FAILURE} otherwise. */ @Nonnull public static ESuccess writeMap (@Nonnull final Map <String, String> aMap, @Nonnull @WillClose final OutputStream aOS) { ValueEnforcer.notNull (aMap, "Map"); ValueEnforcer.notNull (aOS, "OutputStream"); try { final IMicroDocument aDoc = createMapDocument (aMap); return MicroWriter.writeToStream (aDoc, aOS, XMLWriterSettings.DEFAULT_XML_SETTINGS); } finally { StreamHelper.close (aOS); } }
Example #7
Source File: MicroWriterTest.java From ph-commons with Apache License 2.0 | 6 votes |
@Test @Ignore ("Takes too long and was already tested with JDK 1.8 runtime parser") public void testSpecialCharactersXML10Text () { final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.XML_10; final XMLWriterSettings aSettings = new XMLWriterSettings ().setSerializeVersion (eXMLSerializeVersion); for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i) if (!XMLCharHelper.isInvalidXMLTextChar (eXMLSerializeVersion, (char) i)) { final String sText = "abc" + (char) i + "def"; assertEquals (7, sText.length ()); final IMicroDocument aDoc = new MicroDocument (); aDoc.appendElement ("root").appendText (sText); final String sXML = MicroWriter.getNodeAsString (aDoc, aSettings); final IMicroDocument aDoc2 = MicroReader.readMicroXML (sXML); assertNotNull ("Failed to read with byte " + i + "\n" + sXML, aDoc2); assertEquals ("Length for byte " + i, i == 0 ? 6 : 7, aDoc2.getDocumentElement ().getTextContent ().length ()); assertTrue ("Difference in byte 0x" + Integer.toHexString (i), aDoc.isEqualContent (aDoc2)); } }
Example #8
Source File: MicroWriterTest.java From ph-commons with Apache License 2.0 | 6 votes |
@Test public void testOrderAttributes () { XMLWriterSettings aSettings = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE) .setUseDoubleQuotesForAttributes (false); // default order final IMicroElement e = new MicroElement ("a"); e.setAttribute ("c", "1"); e.setAttribute ("b", "2"); e.setAttribute ("a", "3"); assertEquals ("<a c='1' b='2' a='3' />", MicroWriter.getNodeAsString (e, aSettings)); // Lexicographic order aSettings = aSettings.setOrderAttributesAndNamespaces (true); assertEquals ("<a a='3' b='2' c='1' />", MicroWriter.getNodeAsString (e, aSettings)); }
Example #9
Source File: PSPreprocessorTest.java From ph-schematron with Apache License 2.0 | 6 votes |
@Test public void testIssue51 () throws SchematronException { final PSPreprocessor aPreprocessor = new PSPreprocessor (PSXPathQueryBinding.getInstance ()).setKeepTitles (true) .setKeepDiagnostics (true); final IReadableResource aRes = new FileSystemResource ("src/test/resources/issues/github51/test51.sch"); final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes (aRes, true); final PSReader aReader = new PSReader (aRes).setLenient (true); final PSSchema aSchema = aReader.readSchemaFromXML (aDoc.getDocumentElement ()); final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema (aSchema); assertNotNull (aPreprocessedSchema); assertTrue (aPreprocessedSchema.isValid (new DoNothingPSErrorHandler ())); final PSWriterSettings aPWS = new PSWriterSettings (); aPWS.setXMLWriterSettings (new XMLWriterSettings ().setIndent (EXMLSerializeIndent.INDENT_AND_ALIGN) .setPutNamespaceContextPrefixesInRoot (true) .setNamespaceContext (PSWriterSettings.createNamespaceMapping (aPreprocessedSchema))); LOGGER.info ("Preprocessed:\n" + new PSWriter (aPWS).getXMLString (aPreprocessedSchema)); }
Example #10
Source File: Issue48Test.java From ph-schematron with Apache License 2.0 | 6 votes |
public static void validateAndProduceSVRL (@Nonnull final File aSchematron, final File aXML) throws Exception { final PSSchema aSchema = new PSReader (new FileSystemResource (aSchematron)).readSchema (); final PSPreprocessor aPreprocessor = new PSPreprocessor (PSXPathQueryBinding.getInstance ()); final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema (aSchema); final String sSCH = new PSWriter (new PSWriterSettings ().setXMLWriterSettings (new XMLWriterSettings ())).getXMLString (aPreprocessedSchema); if (false) System.out.println (sSCH); final SchematronResourceSCH aSCH = new SchematronResourceSCH (new ReadableResourceString (sSCH, StandardCharsets.UTF_8)); // Perform validation final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (new FileSystemResource (aXML)); assertNotNull (aSVRL); if (false) System.out.println (new SVRLMarshaller ().getAsString (aSVRL)); }
Example #11
Source File: MicroWriterTest.java From ph-commons with Apache License 2.0 | 6 votes |
@Test public void testOrderNamespaces () { XMLWriterSettings aSettings = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE) .setUseDoubleQuotesForAttributes (false); // default order final IMicroElement e = new MicroElement ("urn:stringdefault", "a"); e.setAttribute ("urn:string3", "c", "1"); e.setAttribute ("urn:string2", "b", "2"); e.setAttribute ("urn:string1", "a", "3"); // Attributes are ordered automatically in DOM! assertEquals ("<a xmlns='urn:stringdefault' xmlns:ns0='urn:string3' ns0:c='1' xmlns:ns1='urn:string2' ns1:b='2' xmlns:ns2='urn:string1' ns2:a='3' />", MicroWriter.getNodeAsString (e, aSettings)); aSettings = aSettings.setOrderAttributesAndNamespaces (true); assertEquals ("<a xmlns='urn:stringdefault' xmlns:ns0='urn:string3' xmlns:ns1='urn:string2' xmlns:ns2='urn:string1' ns2:a='3' ns1:b='2' ns0:c='1' />", MicroWriter.getNodeAsString (e, aSettings)); }
Example #12
Source File: MicroWriterTest.java From ph-commons with Apache License 2.0 | 6 votes |
private static void _testC14 (final String sSrc, final String sDst) { final IMicroDocument aDoc = MicroReader.readMicroXML (sSrc, new SAXReaderSettings ().setEntityResolver ( (x, y) -> "world.txt".equals (y) ? new StringSAXInputSource ("world") : new StringSAXInputSource (""))); assertNotNull (aDoc); final MapBasedNamespaceContext aCtx = new MapBasedNamespaceContext (); aCtx.addMapping ("a", "http://www.w3.org"); aCtx.addMapping ("b", "http://www.ietf.org"); final String sC14 = MicroWriter.getNodeAsString (aDoc, XMLWriterSettings.createForCanonicalization () .setIndentationString (" ") .setNamespaceContext (aCtx) .setSerializeComments (EXMLSerializeComments.IGNORE)); assertEquals (sDst, sC14); }
Example #13
Source File: MicroDOMInputStreamProviderTest.java From ph-commons with Apache License 2.0 | 5 votes |
@Test public void testSimple () { final IMicroDocument aDoc = new MicroDocument (); aDoc.appendElement ("test"); assertNotNull (new MicroDOMInputStreamProvider (aDoc, XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ).getInputStream ()); }
Example #14
Source File: MicroWriterTest.java From ph-commons with Apache License 2.0 | 5 votes |
@Test @Ignore ("Takes too long and was already tested with JDK 1.8 runtime parser") public void testSpecialCharactersXML11CDATA () { final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.XML_11; final XMLWriterSettings aSettings = new XMLWriterSettings ().setSerializeVersion (eXMLSerializeVersion); for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i) if (!XMLCharHelper.isInvalidXMLCDATAChar (eXMLSerializeVersion, (char) i)) { final String sText = "abc" + (char) i + "def"; assertEquals (7, sText.length ()); final IMicroDocument aDoc = new MicroDocument (); aDoc.appendElement ("root").appendCDATA (sText); final String sXML = MicroWriter.getNodeAsString (aDoc, aSettings); final IMicroDocument aDoc2 = MicroReader.readMicroXML (sXML); assertNotNull ("Failed to read with byte " + i + "\n" + sXML, aDoc2); assertEquals ("Length for byte " + i, i == 0 ? 6 : 7, aDoc2.getDocumentElement ().getTextContent ().length ()); // Difference between created "\r" and read "\n" // Difference between created "0x2028" and read "\n" if (i != '\r' && i != 0x2028) if (!aDoc.isEqualContent (aDoc2)) { final String sXML2 = MicroWriter.getNodeAsString (aDoc2, aSettings); fail ("0x" + Integer.toHexString (i) + "\n" + sXML + "\n" + sXML2); } } }
Example #15
Source File: MicroWriterTest.java From ph-commons with Apache License 2.0 | 5 votes |
@Test @Ignore ("Takes too long and was already tested with JDK 1.8 runtime parser") public void testSpecialCharactersXML11Text () { final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.XML_11; final XMLWriterSettings aSettings = new XMLWriterSettings ().setSerializeVersion (eXMLSerializeVersion); for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i) if (!XMLCharHelper.isInvalidXMLTextChar (eXMLSerializeVersion, (char) i)) { final String sText = "abc" + (char) i + "def"; assertEquals (7, sText.length ()); final IMicroDocument aDoc = new MicroDocument (); aDoc.appendElement ("root").appendText (sText); final String sXML = MicroWriter.getNodeAsString (aDoc, aSettings); final IMicroDocument aDoc2 = MicroReader.readMicroXML (sXML); assertNotNull ("Failed to read with byte " + i + "\n" + sXML, aDoc2); assertEquals ("Length for byte " + i, i == 0 ? 6 : 7, aDoc2.getDocumentElement ().getTextContent ().length ()); // Difference between created "0x2028" and read "\n" if (i != 0x2028) if (!aDoc.isEqualContent (aDoc2)) { final String sXML2 = MicroWriter.getNodeAsString (aDoc2, aSettings); fail ("0x" + Integer.toHexString (i) + "\n" + sXML + "\n" + sXML2); } } }
Example #16
Source File: MicroWriterTest.java From ph-commons with Apache License 2.0 | 5 votes |
@Test @Ignore ("Takes too long and was already tested with JDK 1.8 runtime parser") public void testSpecialCharactersXML10CDATA () { final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.XML_10; final XMLWriterSettings aSettings = new XMLWriterSettings ().setSerializeVersion (eXMLSerializeVersion); for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i) if (!XMLCharHelper.isInvalidXMLCDATAChar (eXMLSerializeVersion, (char) i)) { final String sText = "abc" + (char) i + "def"; assertEquals (7, sText.length ()); final IMicroDocument aDoc = new MicroDocument (); aDoc.appendElement ("root").appendCDATA (sText); final String sXML = MicroWriter.getNodeAsString (aDoc, aSettings); final IMicroDocument aDoc2 = MicroReader.readMicroXML (sXML); assertNotNull ("Failed to read with byte " + i + "\n" + sXML, aDoc2); assertEquals ("Length for byte " + i, i == 0 ? 6 : 7, aDoc2.getDocumentElement ().getTextContent ().length ()); // Difference between created "\r" and read "\n" if (i != '\r') if (!aDoc.isEqualContent (aDoc2)) { final String sXML2 = MicroWriter.getNodeAsString (aDoc2, aSettings); fail ("0x" + Integer.toHexString (i) + "\n" + sXML + "\n" + sXML2); } } }
Example #17
Source File: MicroReaderTest.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Test: use namespaces all over the place and mix them quite complex */ @Test public void testNamespaces () { final XMLWriterSettings xs = new XMLWriterSettings (); xs.setIndent (EXMLSerializeIndent.NONE); final String s = "<?xml version=\"1.0\"?>" + "<verrryoot>" + "<root xmlns=\"myuri\" xmlns:a='foo'>" + "<child xmlns=\"\">" + "<a:child2>Value text - no entities!</a:child2>" + "</child>" + "</root>" + "</verrryoot>"; final IMicroDocument aDoc = MicroReader.readMicroXML (s); assertNotNull (aDoc); final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream (); new MicroSerializer (xs).write (aDoc, baos); final String sXML = baos.getAsString (StandardCharsets.UTF_8); assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<verrryoot>" + "<root xmlns=\"myuri\">" + "<ns0:child xmlns:ns0=\"\">" + "<ns1:child2 xmlns:ns1=\"foo\">Value text - no entities!</ns1:child2>" + "</ns0:child>" + "</root>" + "</verrryoot>", sXML); }
Example #18
Source File: MicroReaderTest.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Test: Use 2 different namespaces and use them both more than once */ @Test public void testNamespaces2 () { final XMLWriterSettings xs = new XMLWriterSettings (); xs.setIndent (EXMLSerializeIndent.NONE); final String s = "<?xml version=\"1.0\"?>" + "<verrryoot xmlns='uri1'>" + "<root>" + "<child xmlns='uri2'>" + "<child2>Value text - no entities!</child2>" + "</child>" + "</root>" + "</verrryoot>"; final IMicroDocument aDoc = MicroReader.readMicroXML (s); assertNotNull (aDoc); final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream (); new MicroSerializer (xs).write (aDoc, baos); final String sXML = baos.getAsString (StandardCharsets.UTF_8); assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<verrryoot xmlns=\"uri1\">" + "<root>" + "<ns0:child xmlns:ns0=\"uri2\">" + "<ns0:child2>Value text - no entities!</ns0:child2>" + "</ns0:child>" + "</root>" + "</verrryoot>", sXML); }
Example #19
Source File: MicroReaderTest.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Test: declare all namespaces in the root element and use them in nested * elements */ @Test public void testNamespaces3 () { final XMLWriterSettings xs = new XMLWriterSettings (); xs.setIndent (EXMLSerializeIndent.NONE); xs.setUseDoubleQuotesForAttributes (false); final String s = "<?xml version=\"1.0\"?>" + "<verrryoot xmlns='uri1' xmlns:a='uri2'>" + "<root>" + "<a:child>" + "<a:child2>Value text - no entities!</a:child2>" + "</a:child>" + "</root>" + "</verrryoot>"; final IMicroDocument aDoc = MicroReader.readMicroXML (s); assertNotNull (aDoc); final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream (); new MicroSerializer (xs).write (aDoc, baos); final String sXML = baos.getAsString (StandardCharsets.UTF_8); assertEquals ("<?xml version='1.0' encoding='UTF-8'?>" + "<verrryoot xmlns='uri1'>" + "<root>" + "<ns0:child xmlns:ns0='uri2'>" + "<ns0:child2>Value text - no entities!</ns0:child2>" + "</ns0:child>" + "</root>" + "</verrryoot>", sXML); }
Example #20
Source File: MicroWriterTest.java From ph-commons with Apache License 2.0 | 5 votes |
@Test public void testXMLVersion () { for (final EXMLSerializeVersion eVersion : EXMLSerializeVersion.values ()) { final IMicroDocument aDoc = MicroReader.readMicroXML (TEST_XML); final XMLWriterSettings aSettings = new XMLWriterSettings (); aSettings.setSerializeVersion (eVersion); final String sXML = MicroWriter.getNodeAsString (aDoc, aSettings); assertNotNull (sXML); assertTrue (sXML.contains ("version=\"" + eVersion.getXMLVersionOrDefault (EXMLVersion.XML_10).getVersion () + "\"")); } }
Example #21
Source File: AbstractSimpleDAO.java From ph-commons with Apache License 2.0 | 5 votes |
/** * @return The {@link IXMLWriterSettings} to be used to serialize the data. */ @Nonnull @OverrideOnDemand protected IXMLWriterSettings getXMLWriterSettings () { return XMLWriterSettings.DEFAULT_XML_SETTINGS; }
Example #22
Source File: PSWriterSettings.java From ph-schematron with Apache License 2.0 | 5 votes |
@Nonnull public IPSWriterSettings setXMLWriterSettings (@Nonnull final IXMLWriterSettings aXMLWriterSettings) { ValueEnforcer.notNull (aXMLWriterSettings, "XMLWriterSettings"); m_aXMLWriterSettings = new XMLWriterSettings (aXMLWriterSettings); return this; }
Example #23
Source File: PSPreprocessorTest.java From ph-schematron with Apache License 2.0 | 5 votes |
@Test public void testBasic () throws Exception { final PSPreprocessor aPreprocessor = new PSPreprocessor (PSXPathQueryBinding.getInstance ()); for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles ()) { // Resolve all includes final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes (aRes, false); assertNotNull (aDoc); // Read to domain object final PSReader aReader = new PSReader (aRes); final PSSchema aSchema = aReader.readSchemaFromXML (aDoc.getDocumentElement ()); assertNotNull (aSchema); // Ensure the schema is valid final CollectingPSErrorHandler aErrHdl = new CollectingPSErrorHandler (); assertTrue (aRes.getPath (), aSchema.isValid (aErrHdl)); assertTrue (aErrHdl.isEmpty ()); // Convert to minified schema if not-yet minimal final PSSchema aPreprocessedSchema = aPreprocessor.getAsMinimalSchema (aSchema); assertNotNull (aPreprocessedSchema); if (false) { final String sXML = MicroWriter.getNodeAsString (aPreprocessedSchema.getAsMicroElement ()); SimpleFileIO.writeFile (new File ("test-minified", FilenameHelper.getWithoutPath (aRes.getPath ()) + ".min-pure.sch"), sXML, XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ); } // Ensure it is still valid and minimal assertTrue (aRes.getPath (), aPreprocessedSchema.isValid (aErrHdl)); assertTrue (aRes.getPath (), aPreprocessedSchema.isMinimal ()); } }
Example #24
Source File: IJAXBWriter.java From ph-commons with Apache License 2.0 | 5 votes |
/** * @return The XML writer settings to be used based on this writer settings. * Never <code>null</code>. */ @Nonnull default IXMLWriterSettings getXMLWriterSettings () { final XMLWriterSettings ret = new XMLWriterSettings ().setNamespaceContext (getNamespaceContext ()) .setIndent (isFormattedOutput () ? EXMLSerializeIndent.INDENT_AND_ALIGN : EXMLSerializeIndent.NONE); if (hasIndentString ()) ret.setIndentationString (getIndentString ()); if (hasCharset ()) ret.setCharset (getCharset ()); return ret.setNewLineMode (ENewLineMode.DEFAULT) .setIncorrectCharacterHandling (EXMLIncorrectCharacterHandling.DO_NOT_WRITE_LOG_WARNING); }
Example #25
Source File: MicroSerializerTest.java From ph-commons with Apache License 2.0 | 4 votes |
@Test public void testIndent () { final IMicroDocument aDoc = new MicroDocument (); final IMicroElement eHTML = aDoc.appendElement ("html"); final IMicroElement eBody = eHTML.appendElement ("body"); eBody.appendElement ("div"); eBody.appendElement ("span").appendText ("bla"); eBody.appendElement ("span").appendElement ("span").appendElement ("span").setAttribute ("a", 3).appendText (""); final IMicroElement eSpan3 = eBody.appendElement ("span"); eSpan3.appendText ("f"); eSpan3.appendText ("oo"); eSpan3.appendElement ("strong").appendText ("bar"); eSpan3.appendText ("baz"); eBody.appendElement ("div"); final String sCRLF = XMLWriterSettings.DEFAULT_XML_SETTINGS.getNewLineString (); final String s = MicroWriter.getNodeAsString (aDoc, new XMLWriterSettings ().setIndent (EXMLSerializeIndent.INDENT_AND_ALIGN)); assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + sCRLF + "<html>" + sCRLF + " <body>" + sCRLF + " <div />" + sCRLF + " <span>bla</span>" + sCRLF + " <span>" + sCRLF + " <span>" + sCRLF + " <span a=\"3\"></span>" + sCRLF + " </span>" + sCRLF + " </span>" + sCRLF + " <span>foo<strong>bar</strong>baz</span>" + sCRLF + " <div />" + sCRLF + " </body>" + sCRLF + "</html>" + sCRLF, s); }
Example #26
Source File: MainCreateJAXBBinding23.java From ph-ubl with Apache License 2.0 | 4 votes |
public static void main (final String [] args) { // UBL 2.3 { System.out.println ("UBL 2.3"); final IMicroDocument eDoc = _createBaseDoc (); final ICommonsSet <String> aNamespaces = new CommonsHashSet <> (); for (final String sPart : new String [] { "common", "maindoc" }) { final String sBasePath = BASE_XSD_PATH + sPart; for (final File aFile : _getFileList ("src/main" + sBasePath)) { // Each namespace should handled only once final IMicroDocument aDoc = MicroReader.readMicroXML (new FileSystemResource (aFile)); final String sTargetNamespace = _getTargetNamespace (aDoc); if (!aNamespaces.add (sTargetNamespace)) { System.out.println ("Ignored namespace URI " + sTargetNamespace + " in " + aFile.getName ()); continue; } String sPackageName = _convertToPackage (sTargetNamespace); if (sPackageName.endsWith ("_2")) { // Change "_2" to "_23" sPackageName += "3"; } // schemaLocation must be relative to bindings file! final IMicroElement eBindings = eDoc.getDocumentElement () .appendElement (JAXB_NS_URI, "bindings") .setAttribute ("schemaLocation", ".." + sBasePath + "/" + aFile.getName ()) .setAttribute ("node", "/xsd:schema"); eBindings.appendElement (JAXB_NS_URI, "schemaBindings") .appendElement (JAXB_NS_URI, "package") .setAttribute ("name", sPackageName); } } MicroWriter.writeToFile (eDoc, new File (DEFAULT_BINDING_FILE), new XMLWriterSettings ().setIncorrectCharacterHandling (EXMLIncorrectCharacterHandling.DO_NOT_WRITE_LOG_WARNING) .setNamespaceContext (new MapBasedNamespaceContext ().addMapping (XMLConstants.DEFAULT_NS_PREFIX, JAXB_NS_URI) .addMapping ("xsd", XMLConstants.W3C_XML_SCHEMA_NS_URI) .addMapping ("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)) .setPutNamespaceContextPrefixesInRoot (true)); } System.out.println ("Done"); }
Example #27
Source File: SchematronPreprocess.java From ph-schematron with Apache License 2.0 | 4 votes |
@Override public void execute () throws BuildException { boolean bCanRun = false; if (m_aSrcFile == null) _errorOrFail ("No source Schematron file specified!"); else if (m_aSrcFile.exists () && !m_aSrcFile.isFile ()) _errorOrFail ("The specified source Schematron file " + m_aSrcFile + " is not a file!"); else if (m_aDstFile == null) _errorOrFail ("No destination Schematron file specified!"); else if (m_aDstFile.exists () && !m_aDstFile.isFile ()) _errorOrFail ("The specified destination Schematron file " + m_aDstFile + " is not a file!"); else bCanRun = true; if (bCanRun) try { // Read source final PSSchema aSchema = new PSReader (new FileSystemResource (m_aSrcFile)).readSchema (); // Setup preprocessor final PSPreprocessor aPreprocessor = new PSPreprocessor (PSXPathQueryBinding.getInstance ()); aPreprocessor.setKeepTitles (m_bKeepTitles); aPreprocessor.setKeepDiagnostics (m_bKeepDiagnostics); aPreprocessor.setKeepReports (m_bKeepReports); aPreprocessor.setKeepEmptyPatterns (m_bKeepEmptyPatterns); aPreprocessor.setKeepEmptySchema (true); // Main pre-processing final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema (aSchema); // Write the result file new PSWriter (new PSWriterSettings ().setXMLWriterSettings (new XMLWriterSettings ())).writeToFile (aPreprocessedSchema, m_aDstFile); _info ("Successfully pre-processed Schematron " + m_aSrcFile + " to " + m_aDstFile); } catch (final SchematronReadException | SchematronPreprocessException ex) { _errorOrFail ("Error processing Schemtron " + m_aSrcFile.getAbsolutePath (), ex); } }
Example #28
Source File: PSWriterSettings.java From ph-schematron with Apache License 2.0 | 4 votes |
@Nonnull @ReturnsMutableCopy public XMLWriterSettings getXMLWriterSettings () { return new XMLWriterSettings (m_aXMLWriterSettings); }
Example #29
Source File: SchematronPreprocessMojo.java From ph-schematron with Apache License 2.0 | 4 votes |
public void execute () throws MojoExecutionException, MojoFailureException { StaticLoggerBinder.getSingleton ().setMavenLog (getLog ()); if (m_aSourceFile == null) throw new MojoExecutionException ("No Source file specified!"); if (m_aSourceFile.exists () && !m_aSourceFile.isFile ()) throw new MojoExecutionException ("The specified Source file " + m_aSourceFile + " is not a file!"); if (m_aTargetFile == null) throw new MojoExecutionException ("No Target file specified!"); if (m_aTargetFile.exists ()) { if (!m_bOverwriteWithoutNotice) { // 3.1 Not overwriting the existing file getLog ().debug ("Skipping Target file '" + m_aTargetFile.getPath () + "' because it already exists!"); } else { if (!m_aTargetFile.isFile ()) throw new MojoExecutionException ("The specified Target file " + m_aTargetFile + " is not a file!"); } } try { final PSSchema aSchema = new PSReader (new FileSystemResource (m_aSourceFile), null, null).readSchema (); final IPSQueryBinding aQueryBinding = PSQueryBindingRegistry.getQueryBindingOfNameOrThrow (aSchema.getQueryBinding ()); final PSPreprocessor aPreprocessor = new PSPreprocessor (aQueryBinding); aPreprocessor.setKeepTitles (m_bKeepTitles); aPreprocessor.setKeepDiagnostics (m_bKeepDiagnostics); aPreprocessor.setKeepReports (m_bKeepReports); aPreprocessor.setKeepEmptyPatterns (m_bKeepEmptyPatterns); // Pre-process final PSSchema aPreprocessedSchema = aPreprocessor.getForcedPreprocessedSchema (aSchema); if (aPreprocessedSchema == null) throw new SchematronPreprocessException ("Failed to preprocess schema " + aSchema + " with query binding " + aQueryBinding); // Convert to XML string final MapBasedNamespaceContext aNSCtx = new MapBasedNamespaceContext (); aNSCtx.addDefaultNamespaceURI (CSchematron.NAMESPACE_SCHEMATRON); aNSCtx.addMapping ("xsl", CSchematron.NAMESPACE_URI_XSL); aNSCtx.addMapping ("svrl", CSVRL.SVRL_NAMESPACE_URI); // Add all <ns> elements from schema as NS context for (final PSNS aItem : aSchema.getAllNSs ()) aNSCtx.setMapping (aItem.getPrefix (), aItem.getUri ()); final IXMLWriterSettings XWS = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.INDENT_AND_ALIGN) .setNamespaceContext (aNSCtx); final IMicroDocument aDoc = new MicroDocument (); aDoc.appendChild (aPreprocessedSchema.getAsMicroElement ()); if (MicroWriter.writeToFile (aDoc, m_aTargetFile, XWS).isSuccess ()) getLog ().info ("Successfully wrote preprocessed Schematron file '" + m_aTargetFile.getPath () + "'"); else getLog ().error ("Error writing preprocessed Schematron file to '" + m_aTargetFile.getPath () + "'"); } catch (final SchematronException ex) { throw new MojoExecutionException ("Error preprocessing Schematron file '" + m_aSourceFile + "'", ex); } }
Example #30
Source File: MainCreateJAXBBinding22.java From ph-ubl with Apache License 2.0 | 4 votes |
public static void main (final String [] args) { // UBL 2.2 { System.out.println ("UBL 2.2"); final IMicroDocument eDoc = _createBaseDoc (); final ICommonsSet <String> aNamespaces = new CommonsHashSet <> (); for (final String sPart : new String [] { "common", "maindoc" }) { final String sBasePath = BASE_XSD_PATH + sPart; for (final File aFile : _getFileList ("src/main" + sBasePath)) { // Each namespace should handled only once final IMicroDocument aDoc = MicroReader.readMicroXML (new FileSystemResource (aFile)); final String sTargetNamespace = _getTargetNamespace (aDoc); if (!aNamespaces.add (sTargetNamespace)) { System.out.println ("Ignored namespace URI " + sTargetNamespace + " in " + aFile.getName ()); continue; } String sPackageName = _convertToPackage (sTargetNamespace); if (sPackageName.endsWith ("_2")) { // Change "_2" to "_22" sPackageName += "2"; } // schemaLocation must be relative to bindings file! final IMicroElement eBindings = eDoc.getDocumentElement () .appendElement (JAXB_NS_URI, "bindings") .setAttribute ("schemaLocation", ".." + sBasePath + "/" + aFile.getName ()) .setAttribute ("node", "/xsd:schema"); eBindings.appendElement (JAXB_NS_URI, "schemaBindings") .appendElement (JAXB_NS_URI, "package") .setAttribute ("name", sPackageName); } } MicroWriter.writeToFile (eDoc, new File (DEFAULT_BINDING_FILE), new XMLWriterSettings ().setIncorrectCharacterHandling (EXMLIncorrectCharacterHandling.DO_NOT_WRITE_LOG_WARNING) .setNamespaceContext (new MapBasedNamespaceContext ().addMapping (XMLConstants.DEFAULT_NS_PREFIX, JAXB_NS_URI) .addMapping ("xsd", XMLConstants.W3C_XML_SCHEMA_NS_URI) .addMapping ("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)) .setPutNamespaceContextPrefixesInRoot (true)); } System.out.println ("Done"); }