com.helger.xml.serialize.write.EXMLSerializeIndent Java Examples

The following examples show how to use com.helger.xml.serialize.write.EXMLSerializeIndent. 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: PSPreprocessorTest.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: MicroWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
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 #3
Source File: MicroWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
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 #4
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@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: MicroWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: MicroWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@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 #7
Source File: IJAXBWriter.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * @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 #8
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #9
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #10
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #11
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimple ()
{
  final String s = "<?xml version=\"1.0\"?>" +
                   "<verrryoot>" +
                   "<root xmlns=\"myuri\">" +
                   "<child xmlns=\"\">" +
                   "<a:child2 xmlns:a=\"foo\">Value text - no entities!</a:child2>" +
                   "</child>" +
                   "</root>" +
                   "</verrryoot>";
  IMicroDocument aDoc = MicroReader.readMicroXML (s);
  assertNotNull (aDoc);

  aDoc = MicroReader.readMicroXML (new StringSAXInputSource (s));
  assertNotNull (aDoc);

  aDoc = MicroReader.readMicroXML (new NonBlockingStringReader (s));
  assertNotNull (aDoc);

  aDoc = MicroReader.readMicroXML (new StringInputStreamProvider (s, StandardCharsets.ISO_8859_1));
  assertNotNull (aDoc);

  aDoc = MicroReader.readMicroXML (new NonBlockingByteArrayInputStream (s.getBytes (StandardCharsets.ISO_8859_1)));
  assertNotNull (aDoc);

  aDoc = MicroReader.readMicroXML (s, new SAXReaderSettings ().setErrorHandler (new LoggingSAXErrorHandler ()));
  assertNotNull (aDoc);

  final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream ();
  new MicroSerializer ().write (aDoc, baos);
  assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                CRLF +
                "<verrryoot>" +
                CRLF +
                INDENT +
                "<root xmlns=\"myuri\">" +
                CRLF +
                INDENT +
                INDENT +
                "<ns0:child xmlns:ns0=\"\">" +
                CRLF +
                INDENT +
                INDENT +
                INDENT +
                "<ns1:child2 xmlns:ns1=\"foo\">Value text - no entities!</ns1:child2>" +
                CRLF +
                INDENT +
                INDENT +
                "</ns0:child>" +
                CRLF +
                INDENT +
                "</root>" +
                CRLF +
                "</verrryoot>" +
                CRLF,
                baos.getAsString (StandardCharsets.UTF_8));

  final String sXHTML = "<content>" +
                        "<div class=\"css1\">" +
                        "<span class=\"css2\">" +
                        "<span>Text1 " +
                        "<span>Text1b</span>" +
                        "</span>" +
                        " " +
                        "<span>Text1c</span>" +
                        "<span class=\"css3\">" +
                        "<span>Text2</span>" +
                        "</span>" +
                        "</span>" +
                        "</div>" +
                        "</content>";
  final IMicroDocument docXHTML = MicroReader.readMicroXML (new NonBlockingStringReader (sXHTML));
  assertNotNull (docXHTML);
  final String sResult = MicroWriter.getNodeAsString (docXHTML,
                                                      new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE));

  assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<content>" +
                "<div class=\"css1\">" +
                "<span class=\"css2\"><span>Text1 <span>Text1b</span></span> <span>Text1c</span>" +
                "<span class=\"css3\"><span>Text2</span></span>" +
                "</span>" +
                "</div>" +
                "</content>",
                sResult);
}
 
Example #12
Source File: SchematronPreprocessMojo.java    From ph-schematron with Apache License 2.0 4 votes vote down vote up
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 #13
Source File: MicroSerializerTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@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 #14
Source File: MicroReaderTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Test: same as namespace3 test but with a namespace context map
 */
@Test
public void testNamespaces3a ()
{
  final XMLWriterSettings xs = new XMLWriterSettings ();
  xs.setIndent (EXMLSerializeIndent.NONE);
  xs.setUseDoubleQuotesForAttributes (false);
  xs.setNamespaceContext (new MapBasedNamespaceContext ().addMapping ("a1", "uri1").addMapping ("a2", "uri2"));

  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);

  String sXML = MicroWriter.getNodeAsString (aDoc, xs);
  assertEquals ("<?xml version='1.0' encoding='UTF-8'?>" +
                "<a1:verrryoot xmlns:a1='uri1'>" +
                "<a1:root>" +
                "<a2:child xmlns:a2='uri2'>" +
                "<a2:child2>Value text - no entities!</a2:child2>" +
                "</a2:child>" +
                "</a1:root>" +
                "</a1:verrryoot>",
                sXML);

  xs.setPutNamespaceContextPrefixesInRoot (true);
  sXML = MicroWriter.getNodeAsString (aDoc, xs);
  assertEquals ("<?xml version='1.0' encoding='UTF-8'?>" +
                "<a1:verrryoot xmlns:a1='uri1' xmlns:a2='uri2'>" +
                "<a1:root>" +
                "<a2:child>" +
                "<a2:child2>Value text - no entities!</a2:child2>" +
                "</a2:child>" +
                "</a1:root>" +
                "</a1:verrryoot>",
                sXML);
}
 
Example #15
Source File: MicroWriterTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithoutEmitNamespaces ()
{
  final XMLWriterSettings aSettings = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE)
                                                              .setCharset (StandardCharsets.ISO_8859_1);
  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eRoot = aDoc.appendElement ("ns1url", "root");
  eRoot.appendElement ("ns2url", "child1");
  eRoot.appendElement ("ns2url", "child2").setAttribute ("attr1", "a");
  eRoot.appendElement ("ns3url", "child3").setAttribute ("ns3url", "attr1", "a");
  eRoot.appendElement ("ns3url", "child4").setAttribute ("ns4url", "attr1", "a");

  String s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<root xmlns=\"ns1url\">" +
                "<ns0:child1 xmlns:ns0=\"ns2url\" />" +
                "<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</root>",
                s);

  aSettings.setPutNamespaceContextPrefixesInRoot (true);
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<root xmlns=\"ns1url\">" +
                "<ns0:child1 xmlns:ns0=\"ns2url\" />" +
                "<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</root>",
                s);

  aSettings.setPutNamespaceContextPrefixesInRoot (false);
  aSettings.setEmitNamespaces (false);
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<root>" +
                "<child1 />" +
                "<child2 attr1=\"a\" />" +
                "<child3 attr1=\"a\" />" +
                "<child4 attr1=\"a\" />" +
                "</root>",
                s);

  aSettings.setPutNamespaceContextPrefixesInRoot (true);
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<root>" +
                "<child1 />" +
                "<child2 attr1=\"a\" />" +
                "<child3 attr1=\"a\" />" +
                "<child4 attr1=\"a\" />" +
                "</root>",
                s);
}
 
Example #16
Source File: MicroWriterTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithNamespaceContext ()
{
  final XMLWriterSettings aSettings = new XMLWriterSettings ().setIndent (EXMLSerializeIndent.NONE)
                                                              .setCharset (StandardCharsets.ISO_8859_1);
  final IMicroDocument aDoc = new MicroDocument ();
  final IMicroElement eRoot = aDoc.appendElement ("ns1url", "root");
  eRoot.appendElement ("ns2url", "child1");
  eRoot.appendElement ("ns2url", "child2").setAttribute ("attr1", "a");
  eRoot.appendElement ("ns3url", "child3").setAttribute ("ns3url", "attr1", "a");
  eRoot.appendElement ("ns3url", "child4").setAttribute ("ns4url", "attr1", "a");

  String s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<root xmlns=\"ns1url\">" +
                "<ns0:child1 xmlns:ns0=\"ns2url\" />" +
                "<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</root>",
                s);

  final MapBasedNamespaceContext aCtx = new MapBasedNamespaceContext ();
  aCtx.addMapping ("a", "ns1url");
  aSettings.setNamespaceContext (aCtx);
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<a:root xmlns:a=\"ns1url\">" +
                "<ns0:child1 xmlns:ns0=\"ns2url\" />" +
                "<ns0:child2 xmlns:ns0=\"ns2url\" attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</a:root>",
                s);

  // Add mapping to namespace context
  aCtx.addMapping ("xy", "ns2url");
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<a:root xmlns:a=\"ns1url\">" +
                "<xy:child1 xmlns:xy=\"ns2url\" />" +
                "<xy:child2 xmlns:xy=\"ns2url\" attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</a:root>",
                s);

  // Put namespace context mappings in root
  aSettings.setPutNamespaceContextPrefixesInRoot (true);
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<a:root xmlns:a=\"ns1url\" xmlns:xy=\"ns2url\">" +
                "<xy:child1 />" +
                "<xy:child2 attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "</a:root>",
                s);

  eRoot.appendElement ("ns3url", "zz");
  s = MicroWriter.getNodeAsString (aDoc, aSettings);
  assertEquals ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<a:root xmlns:a=\"ns1url\" xmlns:xy=\"ns2url\">" +
                "<xy:child1 />" +
                "<xy:child2 attr1=\"a\" />" +
                "<ns0:child3 xmlns:ns0=\"ns3url\" ns0:attr1=\"a\" />" +
                "<ns0:child4 xmlns:ns0=\"ns3url\" xmlns:ns1=\"ns4url\" ns1:attr1=\"a\" />" +
                "<ns0:zz xmlns:ns0=\"ns3url\" />" +
                "</a:root>",
                s);
}
 
Example #17
Source File: XMLTransformerFactoryTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testSpecialChars () throws Exception
{
  final EXMLVersion eXMLVersion = EXMLVersion.XML_10;
  final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.getFromXMLVersionOrThrow (eXMLVersion);
  final StringBuilder aAttrVal = new StringBuilder ();
  final StringBuilder aText = new StringBuilder ();
  for (char i = 0; i < 256; ++i)
  {
    if (!XMLCharHelper.isInvalidXMLAttributeValueChar (eXMLSerializeVersion, i))
      aAttrVal.append (i);
    if (!XMLCharHelper.isInvalidXMLTextChar (eXMLSerializeVersion, i))
      aText.append (i);
  }

  final Document aDoc = XMLFactory.newDocument (eXMLVersion);
  final Element eRoot = (Element) aDoc.appendChild (aDoc.createElement ("root"));
  eRoot.setAttribute ("test", aAttrVal.toString ());

  final Element e1 = (Element) eRoot.appendChild (aDoc.createElement ("a"));
  e1.appendChild (aDoc.createTextNode (aText.toString ()));

  final Element e2 = (Element) eRoot.appendChild (aDoc.createElement ("b"));
  e2.appendChild (aDoc.createCDATASection ("aaaaaaaaaaa]]>bbbbbbbbbbb]]>ccccccccc"));

  final Element e3 = (Element) eRoot.appendChild (aDoc.createElement ("c"));
  e3.appendChild (aDoc.createCDATASection ("]]>"));

  if (false)
    e3.appendChild (aDoc.createComment ("<!--"));
  e3.appendChild (aDoc.createTextNode ("abc"));
  if (false)
    e3.appendChild (aDoc.createComment ("-->"));

  final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
  XMLTransformerFactory.newTransformer ().transform (new DOMSource (aDoc), new StreamResult (aSW));
  final String sTransform = aSW.getAsString ();

  final Document aDoc2 = DOMReader.readXMLDOM (sTransform);
  final Node e3a = aDoc2.getDocumentElement ().getChildNodes ().item (2);
  aSW.reset ();
  XMLTransformerFactory.newTransformer ().transform (new DOMSource (e3a), new StreamResult (aSW));

  final String sXML = XMLWriter.getNodeAsString (aDoc,
                                                 new XMLWriterSettings ().setIncorrectCharacterHandling (EXMLIncorrectCharacterHandling.WRITE_TO_FILE_NO_LOG)
                                                                         .setIndent (EXMLSerializeIndent.NONE));
  assertNotNull (sXML);

  assertNotNull ("Failed to read: " + sXML, DOMReader.readXMLDOM (sXML));
}
 
Example #18
Source File: JAXBBuilderFuncTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testSafeXMLStreamWriter ()
{
  final com.helger.jaxb.mock.external.MockJAXBArchive aArc = new com.helger.jaxb.mock.external.MockJAXBArchive ();
  aArc.setVersion ("1.23");
  for (int i = 0; i < 2; ++i)
  {
    final com.helger.jaxb.mock.external.MockJAXBCollection aCollection = new com.helger.jaxb.mock.external.MockJAXBCollection ();
    aCollection.setDescription ("InternalDesc-" + i);
    aCollection.setID (i);

    final com.helger.jaxb.mock.external.MockJAXBIssue aIssue = new com.helger.jaxb.mock.external.MockJAXBIssue ();
    aIssue.setTitle (BigDecimal.valueOf (10000 + i));
    aIssue.setSubTitle ("Test" + i);
    aCollection.getIssue ().add (aIssue);

    aArc.getCollection ().add (aCollection);
  }

  final MockExternalArchiveWriterBuilder aWriter = new MockExternalArchiveWriterBuilder ();
  aWriter.setNamespaceContext (new MapBasedNamespaceContext ().addDefaultNamespaceURI ("http://urn.example.org/bla"));

  final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
  aWriter.write (aArc,
                 SafeXMLStreamWriter.create (aSW,
                                             new XMLWriterSettings ().setIndent (EXMLSerializeIndent.ALIGN_ONLY)
                                                                     .setNewLineMode (ENewLineMode.UNIX))
                                    .setDebugMode (true));
  assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Root Version=\"1.23\">\n" +
                "<Collection ID=\"0\" Description=\"InternalDesc-0\">\n" +
                "<Issue ID=\"0\" CollectionID=\"0\" PageCount=\"0\" ArticleCount=\"0\" DirAbsolute=\"0\">\n" +
                "<Title>10000</Title>\n" +
                "<SubTitle>Test0</SubTitle>\n" +
                "</Issue>\n" +
                "</Collection>\n" +
                "<Collection ID=\"1\" Description=\"InternalDesc-1\">\n" +
                "<Issue ID=\"0\" CollectionID=\"0\" PageCount=\"0\" ArticleCount=\"0\" DirAbsolute=\"0\">\n" +
                "<Title>10001</Title>\n" +
                "<SubTitle>Test1</SubTitle>\n" +
                "</Issue>\n" +
                "</Collection>\n" +
                "</Root>",
                aSW.getAsString ());
}