📄 staxbuilder.java
字号:
// if we're at the start then we need to do a next if (event == -1) event = r.next(); while (true) { boolean noadd = false; Content child = null; switch (event) { case XMLStreamConstants.CDATA: child = f.cdata(r.getText()); break; case XMLStreamConstants.SPACE: if (cfgIgnoreWS) { noadd = true; break; } // fall through case XMLStreamConstants.CHARACTERS: /* * Small complication: although (ignorable) white space is * allowed in prolog/epilog, and StAX may report such event, * JDOM barfs if trying to add it. Thus, let's just ignore all * textual stuff outside the tree: */ if (current == null) { noadd = true; break; } child = f.text(r.getText()); break; case XMLStreamConstants.COMMENT: child = f.comment(r.getText()); break; case XMLStreamConstants.END_DOCUMENT: return; case XMLStreamConstants.END_ELEMENT: /** * If current.getParentElement() previously * returned null and we get this event * again we shouldn't bail out with a * NullPointerException */ if(current != null) { current = current.getParentElement(); } noadd = true; break; case XMLStreamConstants.ENTITY_DECLARATION: case XMLStreamConstants.NOTATION_DECLARATION: /* * Shouldn't really get these, but maybe some stream readers do * provide the info. If so, better ignore it -- DTD event should * have most/all we need. */ noadd = true; break; case XMLStreamConstants.ENTITY_REFERENCE: child = f.entityRef(r.getLocalName()); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: child = f.processingInstruction(r.getPITarget(), r.getPIData()); break; case XMLStreamConstants.START_ELEMENT: // Ok, need to add a new element and simulate recursion { Element newElem = null; String nsURI = r.getNamespaceURI(); String elemPrefix = r.getPrefix(); // needed for special // handling of elem's // namespace String ln = r.getLocalName(); if (nsURI == null || nsURI.length() == 0) { if (elemPrefix == null || elemPrefix.length() == 0) { newElem = f.element(ln); } else { /* * Happens when a prefix is bound to the default (empty) * namespace... */ newElem = f.element(ln, elemPrefix, ""); } } else { newElem = f.element(ln, elemPrefix, nsURI); } /* * Let's add element right away (probably have to do it to bind * attribute namespaces, too) */ if (current == null) { // at root doc.setRootElement(newElem); if(additionalNamespaces != null ){ for( Iterator iter = additionalNamespaces .keySet().iterator();iter.hasNext();){ String prefix = (String) iter.next(); String uri = (String) additionalNamespaces .get(prefix); newElem.addNamespaceDeclaration(Namespace.getNamespace(prefix,uri)); } } } else { f.addContent(current, newElem); } // Any declared namespaces? for (int i = 0, len = r.getNamespaceCount(); i < len; ++i) { String prefix = r.getNamespacePrefix(i); Namespace ns = Namespace.getNamespace(prefix, r.getNamespaceURI(i)); // JDOM has special handling for element's "own" ns: if (prefix != null && prefix.equals(elemPrefix)) { ; // already set by when it was constructed... } else { f.addNamespaceDeclaration(newElem, ns); } } // And then the attributes: for (int i = 0, len = r.getAttributeCount(); i < len; ++i) { String prefix = r.getAttributePrefix(i); Namespace ns; if (prefix == null || prefix.length() == 0) { // Attribute not in any namespace ns = Namespace.NO_NAMESPACE; } else { ns = newElem.getNamespace(prefix); } Attribute attr = f.attribute(r.getAttributeLocalName(i), r.getAttributeValue(i), resolveAttrType(r.getAttributeType(i)), ns); f.setAttribute(newElem, attr); } // And then 'push' new element... current = newElem; } // Already added the element, can continue noadd = true; break; case XMLStreamConstants.START_DOCUMENT: /* * This should only be received at the beginning of document... so, * should we indicate the problem or not? */ /* * For now, let it pass: maybe some (broken) readers pass that info * as first event in beginning of doc? */ case XMLStreamConstants.DTD: /* * !!! Note: StAX does not expose enough information about * doctype declaration (specifically, public and system id!); * should (re-)parse information... not yet implemented */ // TBI //continue main_loop; // Should never get these, from a stream reader: /* * (commented out entries are just FYI; default catches them all) */ // case XMLStreamConstants.ATTRIBUTE: // case XMLStreamConstants.NAMESPACE: default: /* throw new XMLStreamException("Unrecognized iterator event type: " + r.getEventType() + "; should not receive such types (broken stream reader?)");*/ break; } if (!noadd && child != null) { if (current == null) { f.addContent(doc, child); } else { f.addContent(current, child); } } if (r.hasNext()) { event = r.next(); } else { break; } } } private static int resolveAttrType(String typeStr) { if (typeStr != null && typeStr.length() > 0) { Integer I = (Integer) attrTypes.get(typeStr); if (I != null) { return I.intValue(); } } return Attribute.UNDECLARED_TYPE; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -