本文整理汇总了Java中org.w3c.dom.ls.DOMImplementationLS.createLSSerializer方法的典型用法代码示例。如果您正苦于以下问题:Java DOMImplementationLS.createLSSerializer方法的具体用法?Java DOMImplementationLS.createLSSerializer怎么用?Java DOMImplementationLS.createLSSerializer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.w3c.dom.ls.DOMImplementationLS
的用法示例。
在下文中一共展示了DOMImplementationLS.createLSSerializer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreateNewItem2Sell
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* Check for DOMErrorHandler handling DOMError. Before fix of bug 4890927
* DOMConfiguration.setParameter("well-formed",true) throws an exception.
*
* @throws Exception If any errors occur.
*/
@Test
public void testCreateNewItem2Sell() throws Exception {
String xmlFile = XML_DIR + "novelsInvalid.xml";
Document document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(xmlFile);
document.getDomConfig().setParameter("well-formed", true);
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
MyDOMOutput domOutput = new MyDOMOutput();
domOutput.setByteStream(System.out);
LSSerializer writer = impl.createLSSerializer();
writer.write(document, domOutput);
}
示例2: main
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
DOMImplementation impl = document.getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSSerializer dsi = implLS.createLSSerializer();
/* We should have here incorrect document without getXmlVersion() method:
* Such Document is generated by replacing the JDK bootclasses with it's
* own Node,Document and DocumentImpl classes (see run.sh). According to
* XERCESJ-1007 the AbstractMethodError should be thrown in such case.
*/
String result = dsi.writeToString(document);
System.out.println("Result:" + result);
}
示例3: testLSInputParsingString
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
@Test
public void testLSInputParsingString() throws Exception {
DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation();
String xml = "<?xml version='1.0'?><test>runDocumentLS_Q6</test>";
LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null);
LSSerializer domSerializer = impl.createLSSerializer();
// turn off xml decl in serialized string for comparison
domSerializer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE);
LSInput src = impl.createLSInput();
src.setStringData(xml);
assertEquals(src.getStringData(), xml);
Document doc = domParser.parse(src);
String result = domSerializer.writeToString(doc);
assertEquals(result, "<test>runDocumentLS_Q6</test>");
}
示例4: format
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
public static String format(String xml) {
try {
final InputSource src = new InputSource(new StringReader(xml));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));
//May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.
return writer.writeToString(document);
} catch (Exception e) {
return xml;
}
}
示例5: transformNonTextNodeToOutputStream
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* Serializes a node using a certain character encoding.
*
* @param node
* DOM node to serialize
* @param os
* output stream, to which the node is serialized
* @param omitXmlDeclaration
* indicator whether to omit the XML declaration or not
* @param encoding
* character encoding, can be <code>null</code>, if
* <code>null</code> then "UTF-8" is used
* @throws Exception
*/
public static void transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration, String encoding)
throws Exception { //NOPMD
// previously we used javax.xml.transform.Transformer, however the JDK xalan implementation did not work correctly with a specified encoding
// therefore we switched to DOMImplementationLS
if (encoding == null) {
encoding = "UTF-8";
}
DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry.getDOMImplementation("LS");
LSOutput lsOutput = domImplementationLS.createLSOutput();
lsOutput.setEncoding(encoding);
lsOutput.setByteStream(os);
LSSerializer lss = domImplementationLS.createLSSerializer();
lss.getDomConfig().setParameter("xml-declaration", !omitXmlDeclaration);
lss.write(node, lsOutput);
}
示例6: testCreateNewItem2SellRetry
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的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(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());
}
示例7: marshall
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* `
* Serialize XML objects
*
* @param xmlObject : XACML or SAML objects to be serialized
* @return serialized XACML or SAML objects
* @throws EntitlementException
*/
private String marshall(XMLObject xmlObject) throws EntitlementException {
try {
doBootstrap();
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
Element element = marshaller.marshall(xmlObject);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
(DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
LSOutput output = impl.createLSOutput();
output.setByteStream(byteArrayOutputStream);
writer.write(element, output);
return byteArrayOutputStream.toString();
} catch (Exception e) {
log.error("Error Serializing the SAML Response");
throw new EntitlementException("Error Serializing the SAML Response", e);
}
}
示例8: marshall
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* Serializes the specified SAML 2.0 based XML content representation to its corresponding actual XML syntax
* representation.
*
* @param xmlObject the SAML 2.0 based XML content object
* @return a {@link String} representation of the actual XML representation of the SAML 2.0 based XML content
* representation
* @throws SSOException if an error occurs during the marshalling process
*/
public static String marshall(XMLObject xmlObject) throws SSOException {
try {
Marshaller marshaller = XMLObjectProviderRegistrySupport.getMarshallerFactory().getMarshaller(xmlObject);
Element element = null;
if (marshaller != null) {
element = marshaller.marshall(xmlObject);
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS implementation = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = implementation.createLSSerializer();
LSOutput output = implementation.createLSOutput();
output.setByteStream(byteArrayOutputStream);
writer.write(element, output);
return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
} catch (ClassNotFoundException | InstantiationException | MarshallingException | IllegalAccessException e) {
throw new SSOException("Error in marshalling SAML 2.0 Assertion", e);
}
}
示例9: load
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
public void load(File f) throws ConfigPersisterException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
String configString = lsSerializer.writeToString(doc);
_config = (Config) _xstream.fromXML(convertToCurrent(configString));
setConfigPath(f);
} catch (Exception e) {
throw new ConfigPersisterException(e);
}
}
示例10: formatHtml
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
protected String formatHtml(String html) throws MojoExecutionException {
try {
InputSource src = new InputSource(new StringReader(html));
Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
Boolean keepDeclaration = Boolean.valueOf(html.startsWith("<?xml"));
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration);
return writer.writeToString(document);
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
示例11: nodeToString
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
protected static String nodeToString(Node n, boolean pretty) {
try {
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("xml-declaration", false);
if (pretty) {
writer.getDomConfig().setParameter("format-pretty-print", true);
}
return writer.writeToString(n);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
示例12: indentXML
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* Method to generated idented XML
* Credit: Steve McLeod and DaoWen.
* Code from http://stackoverflow.com/a/11519668
* @param xml input xml in string format
* @return indented xml in string format
*/
public static String indentXML(String xml) {
try {
final InputSource src = new InputSource(new StringReader(xml));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final Boolean keepDeclaration = xml.startsWith("<?xml");
//May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.
return writer.writeToString(document);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例13: serialize
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的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();
}
示例14: marshall
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
/**
* Serializing a SAML2 object into a String
*
* @param xmlObject object that needs to serialized.
* @return serialized object
* @throws SAMLSSOException
*/
public static String marshall(XMLObject xmlObject) throws SAMLSSOException {
try {
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration
.getMarshallerFactory();
Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
Element element = marshaller.marshall(xmlObject);
ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream();
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
LSOutput output = impl.createLSOutput();
output.setByteStream(byteArrayOutputStrm);
writer.write(element, output);
return byteArrayOutputStrm.toString();
} catch (Exception e) {
log.error("Error Serializing the SAML Response");
throw new SAMLSSOException("Error Serializing the SAML Response", e);
}
}
示例15: serializeDOM_LS
import org.w3c.dom.ls.DOMImplementationLS; //导入方法依赖的package包/类
protected void serializeDOM_LS(Element elt, OutputStream out, boolean pretty) throws LSException
{
DOMImplementationLS impl = (DOMImplementationLS)XMLImplFinder.getDOMImplementation();
// init and configure serializer
LSSerializer serializer = impl.createLSSerializer();
DOMConfiguration config = serializer.getDomConfig();
config.setParameter("format-pretty-print", pretty);
// wrap output stream
LSOutput output = impl.createLSOutput();
output.setByteStream(out);
// launch serialization
serializer.write(elt, output);
}