下面列出了org.w3c.dom.Document#createDocumentFragment ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Node appendNewChild(XMLStreamReader reader, boolean wrapped, Document contextDocument,
XMLStreamWriter writer, Element element) throws XMLStreamException {
StaxUtils.copy(reader, writer);
DocumentFragment result = contextDocument.createDocumentFragment();
Node child = element.getFirstChild();
if (wrapped) {
child = child.getFirstChild();
}
if (child != null && child.getNextSibling() == null) {
return child;
}
while (child != null) {
Node nextChild = child.getNextSibling();
result.appendChild(child);
child = nextChild;
}
return result;
}
/**
* Method createDocumentFragment
*
*
* NEEDSDOC (createDocumentFragment) @return
*/
synchronized public DTM createDocumentFragment()
{
try
{
DocumentBuilderFactory dbf = FactoryImpl.getDOMFactory(super.useServicesMechnism());
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Node df = doc.createDocumentFragment();
return getDTM(new DOMSource(df), true, null, false, false);
}
catch (Exception e)
{
throw new DTMException(e);
}
}
/**
* Method createDocumentFragment
*
*
* NEEDSDOC (createDocumentFragment) @return
*/
synchronized public DTM createDocumentFragment()
{
try
{
DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(super.overrideDefaultParser());
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Node df = doc.createDocumentFragment();
return getDTM(new DOMSource(df), true, null, false, false);
}
catch (Exception e)
{
throw new DTMException(e);
}
}
/**
* Parse the specified input stream in a DOM DocumentFragment, owned by the specified Document.
*
* @param input the InputStream to parse
* @param owningDocument the Document which will own the returned DocumentFragment
* @return a DocumentFragment
* @throws DecryptionException thrown if there is an error parsing the input stream
*/
private DocumentFragment parseInputStream(InputStream input, Document owningDocument) throws DecryptionException {
// Since Xerces currently seems not to handle parsing into a DocumentFragment
// without a bit hackery, use this to simulate, so we can keep the API
// the way it hopefully will look in the future. Obviously this only works for
// input streams containing valid XML instances, not fragments.
Document newDocument = null;
try {
newDocument = parserPool.parse(input);
} catch (XMLParserException e) {
log.error("Error parsing decrypted input stream", e);
throw new DecryptionException("Error parsing input stream", e);
}
Element element = newDocument.getDocumentElement();
owningDocument.adoptNode(element);
DocumentFragment container = owningDocument.createDocumentFragment();
container.appendChild(element);
return container;
}
/**
* Method createDocumentFragment
*
*
* NEEDSDOC (createDocumentFragment) @return
*/
synchronized public DTM createDocumentFragment()
{
try
{
DocumentBuilderFactory dbf = FactoryImpl.getDOMFactory(super.useServicesMechnism());
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Node df = doc.createDocumentFragment();
return getDTM(new DOMSource(df), true, null, false, false);
}
catch (Exception e)
{
throw new DTMException(e);
}
}
/**
* Method createDocumentFragment
*
*
* NEEDSDOC (createDocumentFragment) @return
*/
synchronized public DTM createDocumentFragment()
{
try
{
DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(super.overrideDefaultParser());
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Node df = doc.createDocumentFragment();
return getDTM(new DOMSource(df), true, null, false, false);
}
catch (Exception e)
{
throw new DTMException(e);
}
}
/**
* Method createDocumentFragment
*
*
* NEEDSDOC (createDocumentFragment) @return
*/
synchronized public DTM createDocumentFragment()
{
try
{
DocumentBuilderFactory dbf = FactoryImpl.getDOMFactory(super.useServicesMechnism());
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Node df = doc.createDocumentFragment();
return getDTM(new DOMSource(df), true, null, false, false);
}
catch (Exception e)
{
throw new DTMException(e);
}
}
/**
* Creates a RegXML Fragment, represented an XML DOM Document Fragment
*
* @param group KLV Group for which the Fragment will be generated.
* @param document Document from which the XML DOM Document Fragment will be
* created.
*
* @return XML DOM Document Fragment containing a single RegXML Fragment
*
* @throws KLVException
* @throws com.sandflow.smpte.regxml.FragmentBuilder.RuleException
*/
public DocumentFragment fromTriplet(Group group, Document document) throws KLVException, RuleException {
DocumentFragment df = document.createDocumentFragment();
applyRule3(df, group);
/* NOTE: Hack to clean-up namespace prefixes */
for (Map.Entry<URI, String> entry : nsprefixes.entrySet()) {
((Element) df.getFirstChild()).setAttributeNS(XMLNS_NS, "xmlns:" + entry.getValue(), entry.getKey().toString());
}
return df;
}
public void handleMessage(Message message) throws Fault {
if (binding.getHandlerChain().isEmpty()) {
return;
}
HandlerChainInvoker invoker = getInvoker(message);
if (invoker.getLogicalHandlers().isEmpty()) {
return;
}
XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
Node nd = message.getContent(Node.class);
SOAPMessage m = message.getContent(SOAPMessage.class);
Document document = null;
if (m != null) {
document = m.getSOAPPart();
} else if (nd != null) {
document = nd.getOwnerDocument();
} else {
document = DOMUtils.newDocument();
message.setContent(Node.class, document);
}
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(document.createDocumentFragment());
// Replace stax writer with DomStreamWriter
message.setContent(XMLStreamWriter.class, writer);
message.put(ORIGINAL_WRITER, origWriter);
message.getInterceptorChain().add(ending);
}
/**
* Implemented to replace the content handler currently in use by a
* NodeBuilder.
*
* @param namespaceURI the namespace URI of the matching element, or an
* empty string if the parser is not namespace aware or the element has
* no namespace
* @param name the local name if the parser is namespace aware, or just
* the element name otherwise
* @param attributes The attribute list of this element
* @throws Exception indicates a JAXP configuration problem
*/
@Override
public void begin(String namespaceURI, String name, Attributes attributes)
throws Exception {
XMLReader xmlReader = getDigester().getXMLReader();
Document doc = documentBuilder.newDocument();
NodeBuilder builder = null;
if (nodeType == Node.ELEMENT_NODE) {
Element element = null;
if (getDigester().getNamespaceAware()) {
element =
doc.createElementNS(namespaceURI, name);
for (int i = 0; i < attributes.getLength(); i++) {
element.setAttributeNS(attributes.getURI(i),
attributes.getLocalName(i),
attributes.getValue(i));
}
} else {
element = doc.createElement(name);
for (int i = 0; i < attributes.getLength(); i++) {
element.setAttribute(attributes.getQName(i),
attributes.getValue(i));
}
}
builder = new NodeBuilder(doc, element);
} else {
builder = new NodeBuilder(doc, doc.createDocumentFragment());
}
xmlReader.setContentHandler(builder);
}
public DocumentFragment parse(InputStream is) throws SAXException,
IOException {
// Wrap the fragment in an arbitrary element
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write("<fragment>".getBytes());
int b;
while ((b = is.read()) != -1) {
baos.write(b);
}
baos.write("</fragment>".getBytes());
// parse
Document doc = this.docBuilder.parse(new ByteArrayInputStream(baos
.toByteArray()));
// // Import the nodes of the new document into doc so that they
// // will be compatible with doc
Node node = doc.importNode(doc.getDocumentElement(), true);
// Create the document fragment node to hold the new nodes
DocumentFragment docfrag = doc.createDocumentFragment();
// Move the nodes into the fragment
while (node.hasChildNodes()) {
docfrag.appendChild(node.removeChild(node.getFirstChild()));
}
// Return the fragment
return docfrag;
}
/**
* Implemented to replace the content handler currently in use by a
* NodeBuilder.
*
* @param namespaceURI the namespace URI of the matching element, or an
* empty string if the parser is not namespace aware or the element has
* no namespace
* @param name the local name if the parser is namespace aware, or just
* the element name otherwise
* @param attributes The attribute list of this element
* @throws Exception indicates a JAXP configuration problem
*/
@Override
public void begin(String namespaceURI, String name, Attributes attributes)
throws Exception {
XMLReader xmlReader = getDigester().getXMLReader();
Document doc = documentBuilder.newDocument();
NodeBuilder builder = null;
if (nodeType == Node.ELEMENT_NODE) {
Element element = null;
if (getDigester().getNamespaceAware()) {
element =
doc.createElementNS(namespaceURI, name);
for (int i = 0; i < attributes.getLength(); i++) {
element.setAttributeNS(attributes.getURI(i),
attributes.getLocalName(i),
attributes.getValue(i));
}
} else {
element = doc.createElement(name);
for (int i = 0; i < attributes.getLength(); i++) {
element.setAttribute(attributes.getQName(i),
attributes.getValue(i));
}
}
builder = new NodeBuilder(doc, element);
} else {
builder = new NodeBuilder(doc, doc.createDocumentFragment());
}
xmlReader.setContentHandler(builder);
}
/**
* When this method fires, the digester is told to forward all SAX
* ContentHandler events to the builder object, resulting in a DOM being
* built instead of normal digester rule-handling occurring. When the
* end of the current xml element is encountered, the original content
* handler is restored (expected to be NULL, allowing normal Digester
* operations to continue).
*
* @param namespaceURI the namespace URI of the matching element, or an
* empty string if the parser is not namespace aware or the element has
* no namespace
* @param name the local name if the parser is namespace aware, or just
* the element name otherwise
* @param attributes The attribute list of this element
* @throws Exception indicates a JAXP configuration problem
*/
@Override
public void begin(String namespaceURI, String name, Attributes attributes)
throws Exception {
Document doc = documentBuilder.newDocument();
NodeBuilder builder = null;
if (nodeType == Node.ELEMENT_NODE) {
Element element = null;
if (getDigester().getNamespaceAware()) {
element =
doc.createElementNS(namespaceURI, name);
for (int i = 0; i < attributes.getLength(); i++) {
element.setAttributeNS(attributes.getURI(i),
attributes.getQName(i),
attributes.getValue(i));
}
} else {
element = doc.createElement(name);
for (int i = 0; i < attributes.getLength(); i++) {
element.setAttribute(attributes.getQName(i),
attributes.getValue(i));
}
}
builder = new NodeBuilder(doc, element);
} else {
builder = new NodeBuilder(doc, doc.createDocumentFragment());
}
// the NodeBuilder constructor has already saved the original
// value of the digester's custom content handler (expected to
// be null, but we save it just in case). So now we just
// need to tell the digester to forward events to the builder.
getDigester().setCustomContentHandler(builder);
}
public void testAddNamespaceElement() throws Exception {
String xml = "<a:Box xmlns:a=\"http://example.org/a#\" required=\"true\"><a:widget size=\"10\"> </a:widget><a:grommit id=\"23\"> text </a:grommit></a:Box>";
Document doc = parse(xml);
ObjectFactory of = con.getObjectFactory();
Entity entity = con.addDesignation(of.createObject(), Entity.class);
DocumentFragment frag = doc.createDocumentFragment();
frag.appendChild(doc.getDocumentElement());
entity.setXML(frag);
RepositoryResult<Statement> resuts = con.getStatements(entity.getResource(), pred, null);
String label = resuts.next().getObject().stringValue();
resuts.close();
assertEquals(xml, label);
}
/**
* @param needsDebugData see {@link DomParser#setNeedsDebugData(boolean)}
*/
AbstractElementStack(Document doc, Namespaces ns, boolean needsDebugData) {
this.doc = doc;
this.needsDebugData = needsDebugData;
this.rootElement = doc.createDocumentFragment();
this.openNodes.add(new OpenNode(rootElement, ns, null));
}
private DocumentFragment createTestDocumentFragment(Document document) {
DocumentFragment docFragment = document.createDocumentFragment();
Element elem = document.createElement("dfElement");
elem.appendChild(document.createTextNode("Text in it"));
docFragment.appendChild(elem);
return docFragment;
}
/**
* This method is an extension that implements as a Xalan extension
* the node-set function also found in xt and saxon.
* If the argument is a Result Tree Fragment, then <code>nodeset</code>
* returns a node-set consisting of a single root node as described in
* section 11.1 of the XSLT 1.0 Recommendation. If the argument is a
* node-set, <code>nodeset</code> returns a node-set. If the argument
* is a string, number, or boolean, then <code>nodeset</code> returns
* a node-set consisting of a single root node with a single text node
* child that is the result of calling the XPath string() function on the
* passed parameter. If the argument is anything else, then a node-set
* is returned consisting of a single root node with a single text node
* child that is the result of calling the java <code>toString()</code>
* method on the passed argument.
* Most of the
* actual work here is done in <code>MethodResolver</code> and
* <code>XRTreeFrag</code>.
* @param myProcessor Context passed by the extension processor
* @param rtf Argument in the stylesheet to the nodeset extension function
*
* NEEDSDOC ($objectName$) @return
*/
public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf)
{
String textNodeValue;
if (rtf instanceof NodeIterator)
{
return new NodeSet((NodeIterator) rtf);
}
else
{
if (rtf instanceof String)
{
textNodeValue = (String) rtf;
}
else if (rtf instanceof Boolean)
{
textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str();
}
else if (rtf instanceof Double)
{
textNodeValue = new XNumber(((Double) rtf).doubleValue()).str();
}
else
{
textNodeValue = rtf.toString();
}
// This no longer will work right since the DTM.
// Document myDoc = myProcessor.getContextNode().getOwnerDocument();
Document myDoc = JdkXmlUtils.getDOMDocument();
Text textNode = myDoc.createTextNode(textNodeValue);
DocumentFragment docFrag = myDoc.createDocumentFragment();
docFrag.appendChild(textNode);
return new NodeSet(docFrag);
}
}
/**
* This method is an extension that implements as a Xalan extension
* the node-set function also found in xt and saxon.
* If the argument is a Result Tree Fragment, then <code>nodeset</code>
* returns a node-set consisting of a single root node as described in
* section 11.1 of the XSLT 1.0 Recommendation. If the argument is a
* node-set, <code>nodeset</code> returns a node-set. If the argument
* is a string, number, or boolean, then <code>nodeset</code> returns
* a node-set consisting of a single root node with a single text node
* child that is the result of calling the XPath string() function on the
* passed parameter. If the argument is anything else, then a node-set
* is returned consisting of a single root node with a single text node
* child that is the result of calling the java <code>toString()</code>
* method on the passed argument.
* Most of the
* actual work here is done in <code>MethodResolver</code> and
* <code>XRTreeFrag</code>.
* @param myProcessor Context passed by the extension processor
* @param rtf Argument in the stylesheet to the nodeset extension function
*
* NEEDSDOC ($objectName$) @return
*/
public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf)
{
String textNodeValue;
if (rtf instanceof NodeIterator)
{
return new NodeSet((NodeIterator) rtf);
}
else
{
if (rtf instanceof String)
{
textNodeValue = (String) rtf;
}
else if (rtf instanceof Boolean)
{
textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str();
}
else if (rtf instanceof Double)
{
textNodeValue = new XNumber(((Double) rtf).doubleValue()).str();
}
else
{
textNodeValue = rtf.toString();
}
// This no longer will work right since the DTM.
// Document myDoc = myProcessor.getContextNode().getOwnerDocument();
Document myDoc = getDocument();
Text textNode = myDoc.createTextNode(textNodeValue);
DocumentFragment docFrag = myDoc.createDocumentFragment();
docFrag.appendChild(textNode);
return new NodeSet(docFrag);
}
}
/**
* This method is an extension that implements as a Xalan extension
* the node-set function also found in xt and saxon.
* If the argument is a Result Tree Fragment, then <code>nodeset</code>
* returns a node-set consisting of a single root node as described in
* section 11.1 of the XSLT 1.0 Recommendation. If the argument is a
* node-set, <code>nodeset</code> returns a node-set. If the argument
* is a string, number, or boolean, then <code>nodeset</code> returns
* a node-set consisting of a single root node with a single text node
* child that is the result of calling the XPath string() function on the
* passed parameter. If the argument is anything else, then a node-set
* is returned consisting of a single root node with a single text node
* child that is the result of calling the java <code>toString()</code>
* method on the passed argument.
* Most of the
* actual work here is done in <code>MethodResolver</code> and
* <code>XRTreeFrag</code>.
* @param myProcessor Context passed by the extension processor
* @param rtf Argument in the stylesheet to the nodeset extension function
*
* NEEDSDOC ($objectName$) @return
*/
public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf)
{
String textNodeValue;
if (rtf instanceof NodeIterator)
{
return new NodeSet((NodeIterator) rtf);
}
else
{
if (rtf instanceof String)
{
textNodeValue = (String) rtf;
}
else if (rtf instanceof Boolean)
{
textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str();
}
else if (rtf instanceof Double)
{
textNodeValue = new XNumber(((Double) rtf).doubleValue()).str();
}
else
{
textNodeValue = rtf.toString();
}
// This no longer will work right since the DTM.
// Document myDoc = myProcessor.getContextNode().getOwnerDocument();
Document myDoc = getDocument();
Text textNode = myDoc.createTextNode(textNodeValue);
DocumentFragment docFrag = myDoc.createDocumentFragment();
docFrag.appendChild(textNode);
return new NodeSet(docFrag);
}
}
/**
* This method is an extension that implements as a Xalan extension
* the node-set function also found in xt and saxon.
* If the argument is a Result Tree Fragment, then <code>nodeset</code>
* returns a node-set consisting of a single root node as described in
* section 11.1 of the XSLT 1.0 Recommendation. If the argument is a
* node-set, <code>nodeset</code> returns a node-set. If the argument
* is a string, number, or boolean, then <code>nodeset</code> returns
* a node-set consisting of a single root node with a single text node
* child that is the result of calling the XPath string() function on the
* passed parameter. If the argument is anything else, then a node-set
* is returned consisting of a single root node with a single text node
* child that is the result of calling the java <code>toString()</code>
* method on the passed argument.
* Most of the
* actual work here is done in <code>MethodResolver</code> and
* <code>XRTreeFrag</code>.
* @param myProcessor Context passed by the extension processor
* @param rtf Argument in the stylesheet to the nodeset extension function
*
* NEEDSDOC ($objectName$) @return
*/
public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf)
{
String textNodeValue;
if (rtf instanceof NodeIterator)
{
return new NodeSet((NodeIterator) rtf);
}
else
{
if (rtf instanceof String)
{
textNodeValue = (String) rtf;
}
else if (rtf instanceof Boolean)
{
textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str();
}
else if (rtf instanceof Double)
{
textNodeValue = new XNumber(((Double) rtf).doubleValue()).str();
}
else
{
textNodeValue = rtf.toString();
}
// This no longer will work right since the DTM.
// Document myDoc = myProcessor.getContextNode().getOwnerDocument();
Document myDoc = JdkXmlUtils.getDOMDocument();
Text textNode = myDoc.createTextNode(textNodeValue);
DocumentFragment docFrag = myDoc.createDocumentFragment();
docFrag.appendChild(textNode);
return new NodeSet(docFrag);
}
}