本文整理汇总了Java中org.w3c.dom.DOMConfiguration.canSetParameter方法的典型用法代码示例。如果您正苦于以下问题:Java DOMConfiguration.canSetParameter方法的具体用法?Java DOMConfiguration.canSetParameter怎么用?Java DOMConfiguration.canSetParameter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.w3c.dom.DOMConfiguration
的用法示例。
在下文中一共展示了DOMConfiguration.canSetParameter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toXml
import org.w3c.dom.DOMConfiguration; //导入方法依赖的package包/类
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);
}
示例2: serialize
import org.w3c.dom.DOMConfiguration; //导入方法依赖的package包/类
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();
}
示例3: write
import org.w3c.dom.DOMConfiguration; //导入方法依赖的package包/类
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.");
}
}
示例4: prettyPrintXmlDocument
import org.w3c.dom.DOMConfiguration; //导入方法依赖的package包/类
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.");
}
}
示例5: testCanonicalForm003
import org.w3c.dom.DOMConfiguration; //导入方法依赖的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");
}
示例6: testSplitCDATA001
import org.w3c.dom.DOMConfiguration; //导入方法依赖的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");
}
示例7: testSplitCDATA002
import org.w3c.dom.DOMConfiguration; //导入方法依赖的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");
}
示例8: testWellFormed001
import org.w3c.dom.DOMConfiguration; //导入方法依赖的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");
}
示例9: testWellFormed002
import org.w3c.dom.DOMConfiguration; //导入方法依赖的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");
}
示例10: testECWhitespace001
import org.w3c.dom.DOMConfiguration; //导入方法依赖的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>: 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");
}
示例11: testECWhitespace002
import org.w3c.dom.DOMConfiguration; //导入方法依赖的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");
}
示例12: setParameter
import org.w3c.dom.DOMConfiguration; //导入方法依赖的package包/类
void setParameter(DOMConfiguration config, String name, Object value)
throws ParserConfigurationException
{
if (!config.canSetParameter(name, value))
{
throw new ParserConfigurationException(name);
}
config.setParameter(name, value);
}
示例13: prettyPrintXMLDocument
import org.w3c.dom.DOMConfiguration; //导入方法依赖的package包/类
/**
* Create a pretty String representation of a DOM Document
* @param node
* @return String of the given document
*/
public static String prettyPrintXMLDocument(Node node) {
if (node == null) {
return "";
}
DocumentBuilder builder = null;
try {
builder = getDocumentBuilder();
} catch (ParserConfigurationException exception) {
System.err.println(exception);
return "";
}
DOMImplementation implementation = builder.getDOMImplementation();
Object object = implementation.getFeature("LS", "3.0");
if (!(object instanceof DOMImplementationLS)) {
return "";
}
DOMImplementationLS loadSave = (DOMImplementationLS)object;
LSSerializer serializer = loadSave.createLSSerializer();
DOMConfiguration config = serializer.getDomConfig();
if (config.canSetParameter("format-pretty-print", true)) {
config.setParameter("format-pretty-print", true);
}
LSOutput loadSaveOut = loadSave.createLSOutput();
StringWriter string = new StringWriter();
loadSaveOut.setCharacterStream(string);
loadSaveOut.setEncoding("UTF-8");
serializer.write(node, loadSaveOut);
String result = string.toString();
// String result = serializer.writeToString(node);
return result;
}
示例14: prettyPrint
import org.w3c.dom.DOMConfiguration; //导入方法依赖的package包/类
public void prettyPrint() {
final String xml = xmlPane.getText();
final InputSource src = new InputSource(new StringReader(xml));
org.w3c.dom.Document document;
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src);
} catch (SAXException | IOException | ParserConfigurationException e) {
return;
}
final Pattern p = Pattern.compile("^<\\?xml.*\\?>");
final Matcher m = p.matcher(xml);
final String xmlDecl = (m.find()) ? m.group() : "";
// 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+.
final DOMImplementation domImplementation = document.getImplementation();
if (!domImplementation.hasFeature("LS", "3.0") || !domImplementation.hasFeature("Core", "2.0")) {
return;
}
final DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0");
final LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
final DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
if (domConfiguration.canSetParameter("format-pretty-print", true)) {
domConfiguration.setParameter("format-pretty-print", true);
}
if (domConfiguration.canSetParameter("xml-declaration", false)) {
domConfiguration.setParameter("xml-declaration", false);
}
final LSOutput lsOutput = domImplementationLS.createLSOutput();
lsOutput.setEncoding("UTF-8");
final StringWriter stringWriter = new StringWriter();
if (!xmlDecl.isEmpty()) {
stringWriter.write(xmlDecl);
stringWriter.write("\n");
}
lsOutput.setCharacterStream(stringWriter);
lsSerializer.write(document, lsOutput);
final String xmlOut = stringWriter.toString();
xmlPane.setText(xmlOut);
}
示例15: testCanonicalForm001
import org.w3c.dom.DOMConfiguration; //导入方法依赖的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 contains two subsequent processing
* instrictions, <br>
* <b>name</b>: canonical-form <br>
* <b>value</b>: true. <br>
* <b>Expected results</b>: the subsequent processing instrictions are
* separated with a single line break
*/
@Test
public void testCanonicalForm001() {
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();
ProcessingInstruction pi1 = doc.createProcessingInstruction("target1", "data1");
ProcessingInstruction pi2 = doc.createProcessingInstruction("target2", "data2");
root.appendChild(pi1);
root.appendChild(pi2);
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();
Node child1 = root.getFirstChild();
Node child2 = child1.getNextSibling();
if (child2.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
Assert.fail("the second child is expected to be a" + "single line break, returned: " + child2);
}
// return Status.passed("OK");
}