use of org.w3c.dom.ls.DOMImplementationLS in project openhab1-addons by openhab.
the class Helper method documentToString.
/***
* Helper method which converts XML Document into pretty formatted string
*
* @param doc to convert
* @return converted XML as String
*/
public static String documentToString(Document doc) {
String strMsg = "";
try {
DOMImplementation domImpl = doc.getImplementation();
DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
LSSerializer lsSerializer = domImplLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("format-pretty-print", true);
Writer stringWriter = new StringWriter();
LSOutput lsOutput = domImplLS.createLSOutput();
lsOutput.setEncoding("UTF-8");
lsOutput.setCharacterStream(stringWriter);
lsSerializer.write(doc, lsOutput);
strMsg = stringWriter.toString();
} catch (Exception e) {
logger.warn("Error occured when converting document to string", e);
}
return strMsg;
}
use of org.w3c.dom.ls.DOMImplementationLS in project ddf by codice.
the class SAMLAuthenticationToken method getCredentialsAsXMLString.
@Override
public String getCredentialsAsXMLString() {
String creds = "";
Element element = getSAMLTokenAsElement();
if (element != null) {
DOMImplementationLS lsImpl = (DOMImplementationLS) element.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
if (null != lsImpl) {
LSSerializer serializer = lsImpl.createLSSerializer();
serializer.getDomConfig().setParameter("xml-declaration", //by default its true, so set it to false to get String without xml-declaration
false);
creds = serializer.writeToString(element);
}
LOGGER.trace("XML representation of SAML token: {}", creds);
}
return creds;
}
use of org.w3c.dom.ls.DOMImplementationLS in project cuba by cuba-platform.
the class XMLConverter method parseCommitRequest.
@Override
public CommitRequest parseCommitRequest(String content) {
try {
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS lsImpl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSParser requestConfigParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
// Set options on the parser
DOMConfiguration config = requestConfigParser.getDomConfig();
config.setParameter("validate", Boolean.TRUE);
config.setParameter("element-content-whitespace", Boolean.FALSE);
config.setParameter("comments", Boolean.FALSE);
requestConfigParser.setFilter(new LSParserFilter() {
@Override
public short startElement(Element elementArg) {
return LSParserFilter.FILTER_ACCEPT;
}
@Override
public short acceptNode(Node nodeArg) {
return StringUtils.isBlank(nodeArg.getTextContent()) ? LSParserFilter.FILTER_REJECT : LSParserFilter.FILTER_ACCEPT;
}
@Override
public int getWhatToShow() {
return NodeFilter.SHOW_TEXT;
}
});
LSInput lsInput = lsImpl.createLSInput();
lsInput.setStringData(content);
Document commitRequestDoc = requestConfigParser.parse(lsInput);
Node rootNode = commitRequestDoc.getFirstChild();
if (!"CommitRequest".equals(rootNode.getNodeName()))
throw new IllegalArgumentException("Not a CommitRequest xml passed: " + rootNode.getNodeName());
CommitRequest result = new CommitRequest();
NodeList children = rootNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
String childNodeName = child.getNodeName();
if ("commitInstances".equals(childNodeName)) {
NodeList entitiesNodeList = child.getChildNodes();
Set<String> commitIds = new HashSet<>(entitiesNodeList.getLength());
for (int j = 0; j < entitiesNodeList.getLength(); j++) {
Node idNode = entitiesNodeList.item(j).getAttributes().getNamedItem("id");
if (idNode == null)
continue;
String id = idNode.getTextContent();
if (id.startsWith("NEW-"))
id = id.substring(id.indexOf('-') + 1);
commitIds.add(id);
}
result.setCommitIds(commitIds);
result.setCommitInstances(parseNodeList(result, entitiesNodeList));
} else if ("removeInstances".equals(childNodeName)) {
NodeList entitiesNodeList = child.getChildNodes();
List removeInstances = parseNodeList(result, entitiesNodeList);
result.setRemoveInstances(removeInstances);
} else if ("softDeletion".equals(childNodeName)) {
result.setSoftDeletion(Boolean.parseBoolean(child.getTextContent()));
}
}
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.w3c.dom.ls.DOMImplementationLS in project opennms by OpenNMS.
the class JaxbClassObjectAdapter method unmarshal.
@Override
public Object unmarshal(final Object from) throws Exception {
LOG.trace("unmarshal: from = ({}){}", (from == null ? null : from.getClass()), from);
if (from == null)
return null;
if (from instanceof Node) {
final Node e = (Node) from;
e.normalize();
final String nodeName = e.getNodeName();
final Class<?> clazz = getClassForElement(nodeName);
LOG.trace("class type = {} (node name = {})", clazz, nodeName);
if (clazz == null) {
LOG.warn("Unable to determine object type for node name {}. Known elements include: {}", nodeName, m_knownElementClasses);
return from;
}
final DOMImplementationLS lsImpl = (DOMImplementationLS) e.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer serializer = lsImpl.createLSSerializer();
// by default its true, so set it to false to get String without xml-declaration
serializer.getDomConfig().setParameter("xml-declaration", false);
final String str = serializer.writeToString(e);
return JaxbUtils.unmarshal(clazz, str);
} else {
LOG.error("Unsure how to determine which class to use for unmarshaling object type {}", from.getClass());
throw new IllegalArgumentException("Unsure how to determine which class to use for unmarshaling object type " + from.getClass());
}
}
use of org.w3c.dom.ls.DOMImplementationLS in project dsl-json by ngs-doo.
the class XmlConverter method serialize.
public static void serialize(final Element value, final JsonWriter sw) {
Document document = value.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
LSOutput lsOutput = domImplLS.createLSOutput();
lsOutput.setEncoding("UTF-8");
StringWriter writer = new StringWriter();
lsOutput.setCharacterStream(writer);
serializer.write(document, lsOutput);
StringConverter.serialize(writer.toString(), sw);
}
Aggregations