本文整理汇总了Java中org.w3c.dom.Document.normalizeDocument方法的典型用法代码示例。如果您正苦于以下问题:Java Document.normalizeDocument方法的具体用法?Java Document.normalizeDocument怎么用?Java Document.normalizeDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.w3c.dom.Document
的用法示例。
在下文中一共展示了Document.normalizeDocument方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compare
import org.w3c.dom.Document; //导入方法依赖的package包/类
@Override
protected boolean compare(File baselineFile, File comparisonFile) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setCoalescing(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document baselineXml = builder.parse(baselineFile);
Document comparisonXml = builder.parse(comparisonFile);
baselineXml.normalizeDocument();
comparisonXml.normalizeDocument();
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreWhitespace(true);
return XMLUnit.compareXML(baselineXml, comparisonXml).similar();
} catch (SAXException | IOException | ParserConfigurationException e) {
throw new TransformationUtilityException("An exception happened when comparing the two XML files", e);
}
}
示例2: compare
import org.w3c.dom.Document; //导入方法依赖的package包/类
public void compare ( final Document sourceDoc, final Document signedDoc ) throws Exception
{
final Document d1 = cloneDoc ( sourceDoc );
final Document d2 = cloneDoc ( signedDoc );
final NodeList nl = d2.getElementsByTagNameNS ( XMLSignature.XMLNS, "Signature" );
while ( nl.getLength () > 0 )
{
final Node item = nl.item ( 0 );
item.getParentNode ().removeChild ( item );
}
d1.normalizeDocument ();
d2.normalizeDocument ();
final Element root1 = d1.getDocumentElement ();
final Element root2 = d2.getDocumentElement ();
compareNode ( root1, root2 );
}
示例3: testCreateNewItem2SellRetry
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* 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());
}
示例4: testXIncludeLoopPos
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* Test if xi:include may reference the doc containing the include if the
* parse type is text.
*
* @throws Exception If any errors occur.
*/
@Test(groups = {"readWriteLocalFiles"})
public void testXIncludeLoopPos() throws Exception {
String resultFile = USER_DIR + "doc_xinc_loops.out";
String goldFile = GOLDEN_DIR + "doc_xinc_loopGold.xml";
String xmlFile = XML_DIR + "doc_xinc_loops.xml";
try (FileOutputStream fos = new FileOutputStream(resultFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true);
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(xmlFile));
doc.normalizeDocument();
doc.setXmlStandalone(true);
TransformerFactory.newInstance().newTransformer()
.transform(new DOMSource(doc), new StreamResult(fos));
}
assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
示例5: testECWhitespace002
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* 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");
}
示例6: testWellFormed001
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* 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");
}
示例7: testMain
import org.w3c.dom.Document; //导入方法依赖的package包/类
@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]);
}
示例8: testWellFormed002
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* 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");
}
示例9: createElement
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* Static method to create a DOM element.
*
* @param aDoc is the document
* @param asNameSpaceURI is the name space for the element. Will be null for DOM level 1 elements
* @param asLocalName is the element name
*/
public static Element createElement(Document aDoc,
String asNameSpaceURI,
String asLocalName) {
Element newElem = null;
if (asNameSpaceURI != null) {
newElem = aDoc.createElementNS(asNameSpaceURI, asLocalName);
// create prefix
String[] parts = asNameSpaceURI.split("/");
String lastPart = parts[parts.length - 1];
String prefix;
if ((lastPart.length() > 2) && (!lastPart.contains("."))) {
prefix = lastPart.toLowerCase().substring(0, 3);
}
else {
prefix = "tns";
}
int prefixSuffix = 0;
do {
if (prefixSuffix > 0) {
prefix = prefix + String.valueOf(prefixSuffix);
}
if ((prefixTable.get(prefix) == null) || (prefixTable.get(prefix) == asNameSpaceURI)) {
newElem.setPrefix(prefix);
prefixTable.put(prefix, asNameSpaceURI);
break;
}
else {
prefixSuffix = prefixSuffix + 1;
}
}
while (prefixTable.get(prefix) == null);
aDoc.normalizeDocument();
}
else {
newElem = aDoc.createElement(asLocalName);
}
return newElem;
}
示例10: testCanonicalForm003
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* 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");
}
示例11: testComments001
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* 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");
}
示例12: testComments002
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* 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");
}
示例13: testCheckScreenNameExists
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* Checking for namespace normalization.
* @see <a href="content/screenName.xml">screenName.xml</a> has prefix of
* userName is bound to "http://hibid.com/user" namespace normalization
* will create a namespace of prefix us and attach userEmail.
*
* @throws Exception If any errors occur.
*/
@Test
public void testCheckScreenNameExists() throws Exception {
String resultFile = USER_DIR + "screenName.out";
String xmlFile = XML_DIR + "screenName.xml";
String goldFile = GOLDEN_DIR + "screenNameGold.xml";
String nsTagName = "http://hibid.com/screenName";
String userNs = "http://hibid.com/user";
try (FileOutputStream output = new FileOutputStream(resultFile)) {
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
Document document = builder.parseURI(xmlFile);
NodeList nl = document.getElementsByTagNameNS(nsTagName, "screen-name");
assertEquals(nl.getLength(), 1);
Element screenName = (Element)nl.item(0);
Element userEmail = document.createElementNS(userNs, "userEmail");
assertTrue(userEmail.isDefaultNamespace(userNs));
Text email = document.createTextNode("[email protected]");
userEmail.appendChild(email);
screenName.appendChild(userEmail);
document.normalizeDocument();
MyDOMOutput domoutput = new MyDOMOutput();
domoutput.setByteStream(output);
writer.write(document, domoutput);
}
assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
示例14: testSplitCDATA001
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* 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");
}
示例15: testSplitCDATA002
import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
* 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");
}