本文整理汇总了Java中org.opendaylight.yangtools.util.xml.UntrustedXML类的典型用法代码示例。如果您正苦于以下问题:Java UntrustedXML类的具体用法?Java UntrustedXML怎么用?Java UntrustedXML使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UntrustedXML类属于org.opendaylight.yangtools.util.xml包,在下文中一共展示了UntrustedXML类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSerializeDeserializeAnyXmlNode
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
@Test
public void testSerializeDeserializeAnyXmlNode() throws Exception {
final ByteArrayInputStream is =
new ByteArrayInputStream("<xml><data/></xml>".getBytes(Charset.defaultCharset()));
final Document parse = UntrustedXML.newDocumentBuilder().parse(is);
final AnyXmlNode anyXmlNode = Builders.anyXmlBuilder()
.withNodeIdentifier(id("anyXmlNode"))
.withValue(new DOMSource(parse))
.build();
final byte[] bytes = SerializationUtils.serializeNormalizedNode(anyXmlNode);
final NormalizedNode<?, ?> deserialized = SerializationUtils.deserializeNormalizedNode(bytes);
final DOMSource value = (DOMSource) deserialized.getValue();
final Diff diff = XMLUnit.compareXML((Document) anyXmlNode.getValue().getNode(),
value.getNode().getOwnerDocument());
Assert.assertTrue(diff.toString(), diff.similar());
}
示例2: translateValueByType
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
private Object translateValueByType(final String value, final DataSchemaNode node,
final NamespaceContext namespaceCtx) throws IOException, SAXException, ParserConfigurationException {
if (node instanceof AnyXmlSchemaNode) {
/*
* FIXME: Figure out some YANG extension dispatch, which will
* reuse JSON parsing or XML parsing - anyxml is not well-defined in
* JSON.
*/
final Document doc = UntrustedXML.newDocumentBuilder().parse(new InputSource(new StringReader(value)));
doc.normalize();
return new DOMSource(doc.getDocumentElement());
}
checkArgument(node instanceof TypedDataSchemaNode);
return codecs.codecFor((TypedDataSchemaNode) node).parseValue(namespaceCtx, value);
}
示例3: parseXML
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
private static Element parseXML(final String name, final String xml) {
try {
return UntrustedXML.newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement();
} catch (SAXException | IOException e) {
throw new ComponentDefinitionException(String.format("Error %s parsing XML: %s", name, xml), e);
}
}
示例4: readAnyXmlValue
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
private void readAnyXmlValue(final JsonReader in, final AnyXmlNodeDataWithSchema parent,
final String anyXmlObjectName) throws IOException {
final String anyXmlObjectNS = getCurrentNamespace().toString();
final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
final Element rootElement = doc.createElementNS(anyXmlObjectNS, anyXmlObjectName);
doc.appendChild(rootElement);
traverseAnyXmlValue(in, doc, rootElement);
final DOMSource domSource = new DOMSource(doc.getDocumentElement());
parent.setValue(domSource);
}
示例5: createAnyXmlSimpleValue
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
private static DOMSource createAnyXmlSimpleValue(final String ns, final String name, final String value) {
final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
final Element rootElement = doc.createElementNS(ns, name);
doc.appendChild(rootElement);
final Text textNode = doc.createTextNode(value);
rootElement.appendChild(textNode);
return new DOMSource(doc.getDocumentElement());
}
示例6: createLf14AnyXmlCompositeValue
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
private static DOMSource createLf14AnyXmlCompositeValue(final String ns, final String name) {
final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
final Element rootElement = doc.createElementNS(ns, name);
final Element anyXmlObjectA = doc.createElement("anyxml-object-a");
final Element dataA1 = doc.createElement("data-a1");
final Text dataA1Text = doc.createTextNode("10");
dataA1.appendChild(dataA1Text);
final Element dataA2 = doc.createElement("data-a2");
final Text dataA2Text = doc.createTextNode("11");
dataA2.appendChild(dataA2Text);
anyXmlObjectA.appendChild(dataA1);
anyXmlObjectA.appendChild(dataA2);
final Element anyXmlObjectB = doc.createElement("anyxml-object-b");
final Element childObjectB1 = doc.createElement("child-object-b1");
final Element dataB1 = doc.createElement("data-b1");
final Text dataB1Text = doc.createTextNode("5.5");
dataB1.appendChild(dataB1Text);
childObjectB1.appendChild(dataB1);
final Element childObjectB2 = doc.createElement("child-object-b2");
final Element dataB2 = doc.createElement("data-b2");
final Text dataB2Text = doc.createTextNode("b2-val");
dataB2.appendChild(dataB2Text);
childObjectB2.appendChild(dataB2);
anyXmlObjectB.appendChild(childObjectB1);
anyXmlObjectB.appendChild(childObjectB2);
rootElement.appendChild(anyXmlObjectA);
rootElement.appendChild(anyXmlObjectB);
doc.appendChild(rootElement);
return new DOMSource(doc.getDocumentElement());
}
示例7: testParsingAttributes
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
@Test
public void testParsingAttributes() throws Exception {
final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug8745/foo.yang");
final QName contWithAttributes = QName.create("foo", "cont-with-attributes");
final ContainerSchemaNode contWithAttr = (ContainerSchemaNode) SchemaContextUtil.findDataSchemaNode(
schemaContext, SchemaPath.create(true, contWithAttributes));
final Document doc = loadDocument("/bug8745/foo.xml");
final DOMSource domSource = new DOMSource(doc.getDocumentElement());
final DOMResult domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());
final XMLOutputFactory outputfactory = XMLOutputFactory.newInstance();
outputfactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
final XMLStreamWriter xmlStreamWriter = outputfactory.createXMLStreamWriter(domResult);
final NormalizedNodeStreamWriter streamWriter = XMLStreamNormalizedNodeStreamWriter.create(
xmlStreamWriter, schemaContext);
final InputStream resourceAsStream = Bug8745Test.class.getResourceAsStream("/bug8745/foo.xml");
// final XMLStreamReader reader = inputFactory.createXMLStreamReader(resourceAsStream);
final XMLStreamReader reader = new DOMSourceXMLStreamReader(domSource);
final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, contWithAttr);
xmlParser.parse(reader);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setNormalize(true);
final String expectedXml = toString(doc.getDocumentElement());
final String serializedXml = toString(domResult.getNode());
final Diff diff = new Diff(expectedXml, serializedXml);
XMLAssert.assertXMLEqual(diff, true);
}
示例8: test
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
@Test
public void test() throws Exception {
final SchemaContext schemaContext = YangParserTestUtils.parseYangResourceDirectory("/bug8803");
final SchemaPath topContPath = SchemaPath.create(true, QName.create("foo-ns", "top-cont"));
final SchemaNode dataSchemaNode = SchemaContextUtil.findDataSchemaNode(schemaContext, topContPath);
assertTrue(dataSchemaNode instanceof ContainerSchemaNode);
final ContainerSchemaNode topContSchema = (ContainerSchemaNode) dataSchemaNode;
final InputStream resourceAsStream = Bug8803Test.class.getResourceAsStream("/bug8803/foo.xml");
// deserialization
final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
final NormalizedNodeResult result = new NormalizedNodeResult();
final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topContSchema);
xmlParser.parse(reader);
final NormalizedNode<?, ?> transformedInput = result.getResult();
assertNotNull(transformedInput);
// serialization
final StringWriter writer = new StringWriter();
final XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
// switching NS repairing to false does not help
outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
final XMLStreamWriter xmlStreamWriter = outputFactory.createXMLStreamWriter(writer);
final NormalizedNodeStreamWriter xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(
xmlStreamWriter, schemaContext);
final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
xmlNormalizedNodeStreamWriter);
normalizedNodeWriter.write(transformedInput);
final String serializedXml = writer.toString();
assertFalse(serializedXml.isEmpty());
}
示例9: writeNormalizedNode
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
private static DOMResult writeNormalizedNode(final ContainerNode normalized, final SchemaContext context)
throws IOException, XMLStreamException {
final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
final DOMResult result = new DOMResult(doc);
NormalizedNodeWriter normalizedNodeWriter = null;
NormalizedNodeStreamWriter normalizedNodeStreamWriter = null;
XMLStreamWriter writer = null;
try {
writer = XML_FACTORY.createXMLStreamWriter(result);
normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, context);
normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter);
for (NormalizedNode<?, ?> child : normalized.getValue()) {
normalizedNodeWriter.write(child);
}
normalizedNodeWriter.flush();
} finally {
if (normalizedNodeWriter != null) {
normalizedNodeWriter.close();
}
if (normalizedNodeStreamWriter != null) {
normalizedNodeStreamWriter.close();
}
if (writer != null) {
writer.close();
}
}
return result;
}
示例10: test
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
@Test
public void test() throws Exception {
final SchemaContext schemaContext = YangParserTestUtils.parseYangResourceDirectory("/dom-reader-test");
final ContainerSchemaNode outerContainerSchema = (ContainerSchemaNode) SchemaContextUtil
.findNodeInSchemaContext(schemaContext, ImmutableList.of(QName.create("foo-ns", "top-cont")));
assertNotNull(outerContainerSchema);
// deserialization
final Document doc = loadDocument("/dom-reader-test/foo.xml");
final DOMSource inputXml = new DOMSource(doc.getDocumentElement());
XMLStreamReader domXMLReader = new DOMSourceXMLStreamReader(inputXml);
final NormalizedNodeResult result = new NormalizedNodeResult();
final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema);
xmlParser.parse(domXMLReader);
final NormalizedNode<?, ?> transformedInput = result.getResult();
assertNotNull(transformedInput);
// serialization
//final StringWriter writer = new StringWriter();
final DOMResult domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());
final XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
final XMLStreamWriter xmlStreamWriter = outputFactory.createXMLStreamWriter(domResult);
final NormalizedNodeStreamWriter xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(
xmlStreamWriter, schemaContext);
final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
xmlNormalizedNodeStreamWriter);
normalizedNodeWriter.write(transformedInput);
//final String serializedXml = writer.toString();
final String serializedXml = toString(domResult.getNode());
assertFalse(serializedXml.isEmpty());
}
示例11: transformSource
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
public static YinDomSchemaSource transformSource(final YinTextSchemaSource source) throws SAXException,
IOException {
final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
final SAXParser parser = UntrustedXML.newSAXParser();
final DefaultHandler handler = new StatementSourceReferenceHandler(doc, null);
parser.parse(source.openStream(), handler);
return YinDomSchemaSource.create(source.getIdentifier(), new DOMSource(doc));
}
示例12: createLf12AnyXmlCompositeValue
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
private static DOMSource createLf12AnyXmlCompositeValue(final String ns, final String name) {
final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
final Element rootElement = doc.createElementNS(ns, name);
final Element arrayElement1 = doc.createElement("array-element");
final Text arrayElement1Text = doc.createTextNode("true");
arrayElement1.appendChild(arrayElement1Text);
final Element arrayElement2 = doc.createElement("array-element");
final Element arrayElement2Baz = doc.createElement("baz");
final Element bazArrayElement1 = doc.createElement("array-element");
final Text bazArrayElement1Text = doc.createTextNode("120");
bazArrayElement1.appendChild(bazArrayElement1Text);
final Element bazArrayElement2 = doc.createElement("array-element");
final Text bazArrayElement2Text = doc.createTextNode("str-val");
bazArrayElement2.appendChild(bazArrayElement2Text);
final Element bazArrayElement3 = doc.createElement("array-element");
final Text bazArrayElement3Text = doc.createTextNode("false");
bazArrayElement3.appendChild(bazArrayElement3Text);
arrayElement2Baz.appendChild(bazArrayElement1);
arrayElement2Baz.appendChild(bazArrayElement2);
arrayElement2Baz.appendChild(bazArrayElement3);
arrayElement2.appendChild(arrayElement2Baz);
final Element arrayElement3 = doc.createElement("array-element");
final Element arrayElement3Foo = doc.createElement("foo");
final Text fooText = doc.createTextNode("null");
arrayElement3Foo.appendChild(fooText);
arrayElement3.appendChild(arrayElement3Foo);
rootElement.appendChild(arrayElement1);
rootElement.appendChild(arrayElement2);
rootElement.appendChild(arrayElement3);
doc.appendChild(rootElement);
return new DOMSource(doc.getDocumentElement());
}
示例13: createLf13AnyXmlCompositeValue
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
private static DOMSource createLf13AnyXmlCompositeValue(final String ns, final String name) {
final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
final Element rootElement = doc.createElementNS(ns, name);
final Element anyXmlArrayA = doc.createElement("anyxml-array-a");
final Element arrayAElement1 = doc.createElement("array-element");
final Element arrayAElement1Foo = doc.createElement("foo");
final Text fooText = doc.createTextNode("true");
arrayAElement1Foo.appendChild(fooText);
arrayAElement1.appendChild(arrayAElement1Foo);
final Element arrayAElement2 = doc.createElement("array-element");
final Text arrayAElement2Text = doc.createTextNode("10");
arrayAElement2.appendChild(arrayAElement2Text);
final Element arrayAElement3 = doc.createElement("array-element");
final Element arrayAElement3Bar = doc.createElement("bar");
final Text barText = doc.createTextNode("false");
arrayAElement3Bar.appendChild(barText);
arrayAElement3.appendChild(arrayAElement3Bar);
anyXmlArrayA.appendChild(arrayAElement1);
anyXmlArrayA.appendChild(arrayAElement2);
anyXmlArrayA.appendChild(arrayAElement3);
final Element anyXmlArrayB = doc.createElement("anyxml-array-b");
final Element arrayBElement1 = doc.createElement("array-element");
final Text arrayBElement1Text = doc.createTextNode("1");
arrayBElement1.appendChild(arrayBElement1Text);
final Element arrayBElement2 = doc.createElement("array-element");
final Text arrayBElement2Text = doc.createTextNode("2");
arrayBElement2.appendChild(arrayBElement2Text);
final Element arrayBElement3 = doc.createElement("array-element");
final Element arrayBElement3Element1 = doc.createElement("array-element");
final Text arrayBElement3Element1Text = doc.createTextNode("4");
arrayBElement3Element1.appendChild(arrayBElement3Element1Text);
final Element arrayBElement3Element2 = doc.createElement("array-element");
final Text arrayBElement3Element2Text = doc.createTextNode("5");
arrayBElement3Element2.appendChild(arrayBElement3Element2Text);
arrayBElement3.appendChild(arrayBElement3Element1);
arrayBElement3.appendChild(arrayBElement3Element2);
final Element arrayBElement4 = doc.createElement("array-element");
final Text arrayBElement4Text = doc.createTextNode("7");
arrayBElement4.appendChild(arrayBElement4Text);
anyXmlArrayB.appendChild(arrayBElement1);
anyXmlArrayB.appendChild(arrayBElement2);
anyXmlArrayB.appendChild(arrayBElement3);
anyXmlArrayB.appendChild(arrayBElement4);
rootElement.appendChild(anyXmlArrayA);
rootElement.appendChild(anyXmlArrayB);
doc.appendChild(rootElement);
return new DOMSource(doc.getDocumentElement());
}
示例14: readXmlToDocument
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
doc.getDocumentElement().normalize();
return doc;
}
示例15: readXmlToDocument
import org.opendaylight.yangtools.util.xml.UntrustedXML; //导入依赖的package包/类
private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
doc.getDocumentElement().normalize();
return doc;
}