/** * Obtain a the DOM, level 3, Load/Save serializer {@link LSSerializer} instance from the * given {@link DOMImplementationLS} instance. * * <p> * The serializer instance will be configured with the parameters passed as the <code>serializerParams</code> * argument. It will also be configured with an {@link LSSerializerFilter} that shows all nodes to the filter, * and accepts all nodes shown. * </p> * * @param domImplLS the DOM Level 3 Load/Save implementation to use * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer * instance, obtained via {@link LSSerializer#getDomConfig()}. May be null. * * @return a new LSSerializer instance */ public static LSSerializer getLSSerializer(DOMImplementationLS domImplLS, Map<String, Object> serializerParams) { LSSerializer serializer = domImplLS.createLSSerializer(); serializer.setFilter(new LSSerializerFilter() { public short acceptNode(Node arg0) { return FILTER_ACCEPT; } public int getWhatToShow() { return SHOW_ALL; } }); if (serializerParams != null) { DOMConfiguration serializerDOMConfig = serializer.getDomConfig(); for (String key : serializerParams.keySet()) { serializerDOMConfig.setParameter(key, serializerParams.get(key)); } } return serializer; }
/** * Check for DOMErrorHandler handling DOMError. Before fix of bug 4896132 * test throws DOM Level 1 node error. * * @throws Exception If any errors occur. */ @Test public void testCreateNewItem2SellRetry() throws Exception { String xmlFile = XML_DIR + "accountInfo.xml"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document document = dbf.newDocumentBuilder().parse(xmlFile); DOMConfiguration domConfig = document.getDomConfig(); MyDOMErrorHandler errHandler = new MyDOMErrorHandler(); domConfig.setParameter("error-handler", errHandler); DOMImplementationLS impl = (DOMImplementationLS) DOMImplementationRegistry.newInstance() .getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); MyDOMOutput domoutput = new MyDOMOutput(); domoutput.setByteStream(System.out); writer.write(document, domoutput); document.normalizeDocument(); writer.write(document, domoutput); assertFalse(errHandler.isError()); }
/** * Check for DOMErrorHandler handling DOMError. Before fix of bug 4896132 * test throws DOM Level 1 node error. * * @throws Exception If any errors occur. */ @Test(groups = {"readLocalFiles"}) public void testCreateNewItem2SellRetry() throws Exception { String xmlFile = XML_DIR + "accountInfo.xml"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document document = dbf.newDocumentBuilder().parse(xmlFile); DOMConfiguration domConfig = document.getDomConfig(); MyDOMErrorHandler errHandler = new MyDOMErrorHandler(); domConfig.setParameter("error-handler", errHandler); DOMImplementationLS impl = (DOMImplementationLS) DOMImplementationRegistry.newInstance() .getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); MyDOMOutput domoutput = new MyDOMOutput(); domoutput.setByteStream(System.out); writer.write(document, domoutput); document.normalizeDocument(); writer.write(document, domoutput); assertFalse(errHandler.isError()); }
public static String toXml(Document domDoc) throws TransformerException { DOMImplementation domImplementation = domDoc.getImplementation(); if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) { DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0"); LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); DOMConfiguration domConfiguration = lsSerializer.getDomConfig(); if (domConfiguration.canSetParameter("xml-declaration", Boolean.TRUE)) lsSerializer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE); if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) { lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(domDoc, lsOutput); return stringWriter.toString(); } } return toXml((Node)domDoc); }
private void format(Document document, Writer writer) { DOMImplementation implementation = document.getImplementation(); if(implementation.hasFeature(LS_FEATURE_KEY, LS_FEATURE_VERSION) && implementation.hasFeature(CORE_FEATURE_KEY, CORE_FEATURE_VERSION)) { DOMImplementationLS implementationLS = (DOMImplementationLS) implementation.getFeature(LS_FEATURE_KEY, LS_FEATURE_VERSION); LSSerializer serializer = implementationLS.createLSSerializer(); DOMConfiguration configuration = serializer.getDomConfig(); configuration.setParameter("format-pretty-print", Boolean.TRUE); configuration.setParameter("comments", preserveComments); LSOutput output = implementationLS.createLSOutput(); output.setEncoding("UTF-8"); output.setCharacterStream(writer); serializer.write(document, output); } }
public XSParser(LSResourceResolver entityResolver, DOMErrorHandler errorHandler){ System.setProperty(DOMImplementationRegistry.PROPERTY, DOMXSImplementationSourceImpl.class.getName()); DOMImplementationRegistry registry; try{ registry = DOMImplementationRegistry.newInstance(); }catch(Exception ex){ throw new ImpossibleException(ex); } XSImplementationImpl xsImpl = (XSImplementationImpl)registry.getDOMImplementation("XS-Loader"); xsLoader = xsImpl.createXSLoader(null); DOMConfiguration config = xsLoader.getConfig(); config.setParameter(Constants.DOM_VALIDATE, Boolean.TRUE); if(entityResolver!=null) config.setParameter(Constants.DOM_RESOURCE_RESOLVER, entityResolver); if(errorHandler!=null) config.setParameter(Constants.DOM_ERROR_HANDLER, errorHandler); }
public static String serialize(Document document, boolean prettyPrint) { DOMImplementationLS impl = getDOMImpl(); LSSerializer serializer = impl.createLSSerializer(); // document.normalizeDocument(); DOMConfiguration config = serializer.getDomConfig(); if (prettyPrint && config.canSetParameter("format-pretty-print", Boolean.TRUE)) { config.setParameter("format-pretty-print", true); } config.setParameter("xml-declaration", true); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); Writer writer = new StringWriter(); output.setCharacterStream(writer); serializer.write(document, output); return writer.toString(); }
void write( Writer writer ) { // Pretty-prints a DOM document to XML using DOM Load and Save's LSSerializer. // Note that the "format-pretty-print" DOM configuration parameter can only be set in JDK 1.6+. DOMImplementation domImplementation = doc .getImplementation(); if (domImplementation .hasFeature("LS", "3.0") && domImplementation .hasFeature( "Core", "2.0" ) ) { DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation .getFeature( "LS", "3.0" ); LSSerializer lsSerializer = domImplementationLS .createLSSerializer(); DOMConfiguration domConfiguration = lsSerializer .getDomConfig(); if (domConfiguration .canSetParameter( "format-pretty-print", Boolean.TRUE )) { lsSerializer .getDomConfig() .setParameter( "format-pretty-print", Boolean.TRUE ); LSOutput lsOutput = domImplementationLS .createLSOutput(); lsOutput .setEncoding( "UTF-8" ); lsOutput .setCharacterStream( writer ); lsSerializer.write( doc, lsOutput ); } else { throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable."); } } else { throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported."); } }
private static String prettyPrintXmlDocument(Document document) { // Pretty-prints a DOM document to XML using DOM Load and Save's LSSerializer. // Note that the "format-pretty-print" DOM configuration parameter can only be set in JDK 1.6+. DOMImplementation domImplementation = document.getImplementation(); if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) { DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0"); LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); DOMConfiguration domConfiguration = lsSerializer.getDomConfig(); if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) { lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(document, lsOutput); return stringWriter.toString(); } else { throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable."); } } else { throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported."); } }
/** * Equivalence class partitioning with state and input values orientation * for public void setParameter(String name, Object value) throws * DOMException, <br> * <b>pre-conditions</b>: the doc's root element contains superfluous * namespace declarations, <br> * <b>name</b>: canonical-form <br> * <b>value</b>: true. <br> * <b>Expected results</b>: the superfluous namespace declarations are * removed */ @Test public void testCanonicalForm003() { DOMImplementation domImpl = null; try { domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); } catch (ParserConfigurationException pce) { Assert.fail(pce.toString()); } catch (FactoryConfigurationError fce) { Assert.fail(fce.toString()); } Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); DOMConfiguration config = doc.getDomConfig(); Element root = doc.getDocumentElement(); String XMLNS = "http://www.w3.org/2000/xmlns/"; root.setAttributeNS(XMLNS, "xmlns:extra1", "ExtraNS1"); root.setAttributeNS(XMLNS, "xmlns:extra2", "ExtraNS2"); if (!config.canSetParameter("canonical-form", Boolean.TRUE)) { System.out.println("OK, setting 'canonical-form' to true is not supported"); return; } config.setParameter("canonical-form", Boolean.TRUE); setHandler(doc); doc.normalizeDocument(); String xmlns2 = root.getAttributeNS(XMLNS, "extra1"); if (xmlns2 == null || xmlns2.length() != 0) { Assert.fail("superfluous namespace declarations is not removed: xmlns:extra2 = " + xmlns2); } return; // Status.passed("OK"); }
/** * Equivalence class partitioning with state and input values orientation * for public void setParameter(String name, Object value) throws * DOMException, <br> * <b>pre-conditions</b>: the root element has one CDATASection followed by * one Text node, <br> * <b>name</b>: cdata-sections <br> * <b>value</b>: true. <br> * <b>Expected results</b>: the CDATASection is left intact */ @Test public void testCdataSections001() { DOMImplementation domImpl = null; try { domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); } catch (ParserConfigurationException pce) { Assert.fail(pce.toString()); } catch (FactoryConfigurationError fce) { Assert.fail(fce.toString()); } Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); String cdataText = "CDATA CDATA CDATA"; String textText = "text text text"; CDATASection cdata = doc.createCDATASection(cdataText); Text text = doc.createTextNode(textText); DOMConfiguration config = doc.getDomConfig(); config.setParameter("cdata-sections", Boolean.TRUE); Element root = doc.getDocumentElement(); root.appendChild(cdata); root.appendChild(text); setHandler(doc); doc.normalizeDocument(); Node returned = root.getFirstChild(); if (returned.getNodeType() != Node.CDATA_SECTION_NODE) { Assert.fail("reurned: " + returned + ", expected: CDATASection"); } return; // Status.passed("OK"); }
/** * Equivalence class partitioning with state and input values orientation * for public void setParameter(String name, Object value) throws * DOMException, <br> * <b>pre-conditions</b>: the root element has two Comment nodes, <br> * <b>name</b>: comments <br> * <b>value</b>: true. <br> * <b>Expected results</b>: the Comment nodes belong to the root element */ @Test public void testComments001() { DOMImplementation domImpl = null; try { domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); } catch (ParserConfigurationException pce) { Assert.fail(pce.toString()); } catch (FactoryConfigurationError fce) { Assert.fail(fce.toString()); } Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); Comment comment1 = doc.createComment("comment1"); Comment comment2 = doc.createComment("comment2"); DOMConfiguration config = doc.getDomConfig(); config.setParameter("comments", Boolean.TRUE); Element root = doc.getDocumentElement(); root.appendChild(comment1); root.appendChild(comment2); setHandler(doc); doc.normalizeDocument(); if (comment1.getParentNode() != root) { Assert.fail("comment1 is attached to " + comment1.getParentNode() + ", but expected to be a child of root"); } if (comment2.getParentNode() != root) { Assert.fail("comment1 is attached to " + comment2.getParentNode() + ", but expected to be a child of root"); } return; // Status.passed("OK"); }
/** * Equivalence class partitioning with state and input values orientation * for public void setParameter(String name, Object value) throws * DOMException, <br> * <b>pre-conditions</b>: the root element has two Comment nodes, <br> * <b>name</b>: comments <br> * <b>value</b>: false. <br> * <b>Expected results</b>: the root element has no children */ @Test public void testComments002() { DOMImplementation domImpl = null; try { domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); } catch (ParserConfigurationException pce) { Assert.fail(pce.toString()); } catch (FactoryConfigurationError fce) { Assert.fail(fce.toString()); } Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); Comment comment1 = doc.createComment("comment1"); Comment comment2 = doc.createComment("comment2"); DOMConfiguration config = doc.getDomConfig(); config.setParameter("comments", Boolean.FALSE); Element root = doc.getDocumentElement(); root.appendChild(comment1); root.appendChild(comment2); doc.normalizeDocument(); if (root.getFirstChild() != null) { Assert.fail("root has a child " + root.getFirstChild() + ", but expected to has none"); } return; // Status.passed("OK"); }
/** * Equivalence class partitioning with state and input values orientation * for public void setParameter(String name, Object value) throws * DOMException, <br> * <b>pre-conditions</b>: The root contains a CDATASection with the * termination marker ']]>', <br> * <b>name</b>: split-cdata-sections <br> * <b>value</b>: true. <br> * <b>Expected results</b>: A warning is reported when the section is * splitted */ @Test public void testSplitCDATA001() { DOMImplementation domImpl = null; try { domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); } catch (ParserConfigurationException pce) { Assert.fail(pce.toString()); } catch (FactoryConfigurationError fce) { Assert.fail(fce.toString()); } Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); DOMConfiguration config = doc.getDomConfig(); CDATASection cdata = doc.createCDATASection("text]" + "]>text"); doc.getDocumentElement().appendChild(cdata); TestHandler testHandler = new TestHandler(); config.setParameter("error-handler", testHandler); if (!config.canSetParameter("split-cdata-sections", Boolean.TRUE)) { Assert.fail("cannot set the parameters 'split-cdata-sections' to true"); } config.setParameter("split-cdata-sections", Boolean.TRUE); doc.normalizeDocument(); if (null == testHandler.getWarning()) { Assert.fail("no warning is reported"); } return; // Status.passed("OK"); }
/** * Equivalence class partitioning with state and input values orientation * for public void setParameter(String name, Object value) throws * DOMException, <br> * <b>pre-conditions</b>: The root contains a CDATASection with the * termination marker ']]>', <br> * <b>name</b>: split-cdata-sections <br> * <b>value</b>: false. <br> * <b>Expected results</b>: No warning is reported */ @Test public void testSplitCDATA002() { DOMImplementation domImpl = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(true); domImpl = dbf.newDocumentBuilder().getDOMImplementation(); } catch (ParserConfigurationException pce) { Assert.fail(pce.toString()); } catch (FactoryConfigurationError fce) { Assert.fail(fce.toString()); } Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); DOMConfiguration config = doc.getDomConfig(); CDATASection cdata = doc.createCDATASection("text]" + "]>text"); doc.getDocumentElement().appendChild(cdata); TestHandler testHandler = new TestHandler(); config.setParameter("error-handler", testHandler); if (!config.canSetParameter("split-cdata-sections", Boolean.FALSE)) { Assert.fail("cannot set the parameters 'split-cdata-sections' to false"); } config.setParameter("split-cdata-sections", Boolean.FALSE); doc.normalizeDocument(); if (null == testHandler.getError()) { Assert.fail("no error is reported"); } return; // Status.passed("OK"); }
/** * Equivalence class partitioning with state and input values orientation * for public void setParameter(String name, Object value) throws * DOMException, <br> * <b>pre-conditions</b>: the attribute has EntityReference to '<', <br> * <b>name</b>: well-formed <br> * <b>value</b>: true. <br> * <b>Expected results</b>: An error is reported */ @Test public void testWellFormed001() { Document doc = null; try { doc = loadDocument(null, test2_xml); } catch (Exception e) { Assert.fail(e.getMessage()); } DOMConfiguration config = doc.getDomConfig(); if (!config.canSetParameter("well-formed", Boolean.TRUE)) { Assert.fail("setting 'well-formed' to true is not supported"); } config.setParameter("well-formed", Boolean.TRUE); Element root = doc.getDocumentElement(); Attr attr = doc.createAttributeNS(null, "attr"); try { attr.appendChild(doc.createEntityReference("<")); } catch (DOMException domException) { System.out.println("testWellFormed001: Expected DOMException for Attribute value = '<'" + domException.toString()); return; // OK } root.setAttributeNode(attr); TestHandler testHandler = new TestHandler(); config.setParameter("error-handler", testHandler); doc.normalizeDocument(); if (testHandler.getError() == null && null == testHandler.getFatalError()) { Assert.fail("no error was reported when attribute has <"); } return; // Status.passed("OK"); }
/** * Equivalence class partitioning with state and input values orientation * for public void setParameter(String name, Object value) throws * DOMException, <br> * <b>pre-conditions</b>: the attribute has EntityReference to '<', <br> * <b>name</b>: well-formed <br> * <b>value</b>: false. <br> * <b>Expected results</b>: No error is reported */ @Test public void testWellFormed002() { Document doc = null; try { doc = loadDocument(null, test2_xml); } catch (Exception e) { Assert.fail(e.getMessage()); } DOMConfiguration config = doc.getDomConfig(); if (!config.canSetParameter("well-formed", Boolean.FALSE)) { System.out.println("OK, setting 'well-formed' to false is not supported"); return; } config.setParameter("well-formed", Boolean.FALSE); Element root = doc.getDocumentElement(); Attr attr = doc.createAttributeNS(null, "attr"); attr.appendChild(doc.createEntityReference("x")); root.setAttributeNode(attr); TestHandler testHandler = new TestHandler(); config.setParameter("error-handler", testHandler); doc.normalizeDocument(); if (testHandler.getError() != null || null != testHandler.getFatalError()) { Assert.fail("unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError()); } return; // Status.passed("OK"); }
/** * Equivalence class partitioning with state and input values orientation * for public void setParameter(String name, Object value) throws * DOMException, <br> * <b>pre-conditions</b>: the document root element has a text node with * four white space characters, <br> * <b>name</b>: element-content-whitespace <br> * <b>value</b>: true. <br> * <b>Expected results</b>: the text node is preserved */ @Test public void testECWhitespace001() { Document doc = null; try { doc = loadDocument(null, test3_xml); } catch (Exception e) { Assert.fail(e.getMessage()); } Element root = doc.getDocumentElement(); Text text = doc.createTextNode("\t\n\r "); root.appendChild(text); DOMConfiguration config = doc.getDomConfig(); if (!config.canSetParameter("element-content-whitespace", Boolean.TRUE)) { Assert.fail("setting 'element-content-whitespace' to true is not supported"); } config.setParameter("element-content-whitespace", Boolean.TRUE); if (!config.canSetParameter("validate", Boolean.TRUE)) { System.out.println("OK, setting 'validate' to true is not supported"); return; } config.setParameter("validate", Boolean.TRUE); setHandler(doc); doc.normalizeDocument(); Node firstChild = root.getFirstChild(); if (firstChild == null || firstChild.getNodeType() != Node.TEXT_NODE || !((Text) firstChild).isElementContentWhitespace()) { Assert.fail("the first child is " + firstChild + ", expected a text node with the four whitespace characters"); } return; // Status.passed("OK"); }
/** * Equivalence class partitioning with state and input values orientation * for public void setParameter(String name, Object value) throws * DOMException, <br> * <b>pre-conditions</b>: the document root element has a text node with * four white space characters, <br> * <b>name</b>: element-content-whitespace <br> * <b>value</b>: false. <br> * <b>Expected results</b>: the text node is discarded */ @Test public void testECWhitespace002() { Document doc = null; try { doc = loadDocument(null, test3_xml); } catch (Exception e) { Assert.fail(e.getMessage()); } Element root = doc.getDocumentElement(); Text text = doc.createTextNode("\t\n\r "); root.appendChild(text); DOMConfiguration config = doc.getDomConfig(); if (!config.canSetParameter("element-content-whitespace", Boolean.FALSE)) { System.out.println("OK, setting 'element-content-whitespace' to false is not supported"); return; } config.setParameter("element-content-whitespace", Boolean.FALSE); if (!config.canSetParameter("validate", Boolean.TRUE)) { System.out.println("OK, setting 'validate' to true is not supported"); return; } config.setParameter("validate", Boolean.TRUE); setHandler(doc); doc.normalizeDocument(); Node firstChild = root.getFirstChild(); if (firstChild != null) { Assert.fail("the first child is " + firstChild + ", but no child is expected"); } return; // Status.passed("OK"); }
@Test public void testMain() throws Exception { final boolean[] hadError = new boolean[1]; DocumentBuilderFactory docBF = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBF.newDocumentBuilder(); Document doc = docBuilder.getDOMImplementation().createDocument("namespaceURI", "ns:root", null); CDATASection cdata = doc.createCDATASection("text1]]>text2"); doc.getDocumentElement().appendChild(cdata); DOMConfiguration config = doc.getDomConfig(); DOMErrorHandler erroHandler = new DOMErrorHandler() { public boolean handleError(DOMError error) { System.out.println(error.getMessage()); Assert.assertEquals(error.getType(), "cdata-sections-splitted"); Assert.assertFalse(hadError[0], "two errors were reported"); hadError[0] = true; return false; } }; config.setParameter("error-handler", erroHandler); doc.normalizeDocument(); Assert.assertTrue(hadError[0]); }
/** * <b>DOM</b>: Implements {@link org.w3c.dom.Document#getDomConfig()}. */ public DOMConfiguration getDomConfig() { if (domConfig == null) { domConfig = new DocumentConfiguration(); } return domConfig; }
void setParameter(DOMConfiguration config, String name, Object value) throws ParserConfigurationException { if (!config.canSetParameter(name, value)) { throw new ParserConfigurationException(name); } config.setParameter(name, value); }
public DOMConfiguration getDomConfig() { if (config == null) { config = new DomDocumentConfiguration(); } return config; }
public DOMConfiguration getDomConfig() { // dynamic load to support jre 1.4 and 1.5 try { Method m = doc.getClass().getMethod("getDomConfig", new Class[]{}); return (DOMConfiguration) m.invoke(doc, ArrayUtil.OBJECT_EMPTY); } catch (Exception e) { throw new PageRuntimeException(Caster.toPageException(e)); } }