📄 staxutils.java
字号:
{ writer.writeCData(((CDATASection) n).getData()); } else if ( n instanceof Comment ) { writer.writeComment(((Comment) n).getData()); } else if ( n instanceof EntityReference ) { writer.writeEntityRef(((EntityReference)n).getNodeValue()); } else if ( n instanceof ProcessingInstruction ) { ProcessingInstruction pi = (ProcessingInstruction) n; writer.writeProcessingInstruction(pi.getTarget(), pi.getData()); } } public static Document read(DocumentBuilder builder, XMLStreamReader reader, boolean repairing) throws XMLStreamException { Document doc = builder.newDocument(); readDocElements(doc, reader, repairing); return doc; } /** * @param parent * @return */ private static Document getDocument(Node parent) { return (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument(); } /** * @param parent * @param reader * @return * @throws XMLStreamException */ private static Element startElement(Node parent, XMLStreamReader reader, boolean repairing) throws XMLStreamException { Document doc = getDocument(parent); Element e = doc.createElementNS(reader.getNamespaceURI(), reader.getLocalName()); if (reader.getPrefix() != null && reader.getPrefix() != "") { e.setPrefix(reader.getPrefix()); } parent.appendChild(e); for (int ns = 0; ns < reader.getNamespaceCount(); ns++) { String uri = reader.getNamespaceURI(ns); String prefix = reader.getNamespacePrefix(ns); declare(e, uri, prefix); } for (int att = 0; att < reader.getAttributeCount(); att++) { String name = reader.getAttributeLocalName(att); String prefix = reader.getAttributePrefix(att); if (prefix != null && prefix.length() > 0) name = prefix + ":" + name; Attr attr = doc.createAttributeNS(reader.getAttributeNamespace(att), name); attr.setValue(reader.getAttributeValue(att)); e.setAttributeNode(attr); } reader.next(); readDocElements(e, reader, repairing); if (repairing && !isDeclared(e, reader.getNamespaceURI(), reader.getPrefix())) { declare(e, reader.getNamespaceURI(), reader.getPrefix()); } return e; } private static boolean isDeclared(Element e, String namespaceURI, String prefix) { Attr att; if (prefix != null && prefix.length() > 0) { att = e.getAttributeNodeNS(XML_NS, "xmlns:" + prefix); } else { att = e.getAttributeNode("xmlns"); } if (att != null && att.getNodeValue().equals(namespaceURI)) return true; if (e.getParentNode() instanceof Element) return isDeclared((Element) e.getParentNode(), namespaceURI, prefix); return false; } /** * @param parent * @param reader * @throws XMLStreamException */ public static void readDocElements(Node parent, XMLStreamReader reader, boolean repairing) throws XMLStreamException { Document doc = getDocument(parent); int event = reader.getEventType(); while (reader.hasNext()) { switch (event) { case XMLStreamConstants.START_ELEMENT: startElement(parent, reader, repairing); if (parent instanceof Document) { if (reader.hasNext()) reader.next(); return; } break; case XMLStreamConstants.END_ELEMENT: return; case XMLStreamConstants.NAMESPACE: break; case XMLStreamConstants.ATTRIBUTE: break; case XMLStreamConstants.CHARACTERS: if (parent != null) { parent.appendChild(doc.createTextNode(reader.getText())); } break; case XMLStreamConstants.COMMENT: if (parent != null) { parent.appendChild(doc.createComment(reader.getText())); } break; case XMLStreamConstants.CDATA: parent.appendChild(doc.createCDATASection(reader.getText())); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData())); break; case XMLStreamConstants.ENTITY_REFERENCE: parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData())); break; default: break; } if (reader.hasNext()) { event = reader.next(); } } } private static void declare(Element node, String uri, String prefix) { if (prefix != null && prefix.length() > 0) { node.setAttributeNS(XML_NS, "xmlns:" + prefix, uri); } else { if (uri != null /*&& uri.length() > 0*/) { node.setAttributeNS(XML_NS, "xmlns", uri); } } } /** * @param out * @param encoding * @return */ public static XMLStreamWriter createXMLStreamWriter(OutputStream out, String encoding,MessageContext ctx) { XMLOutputFactory factory = getXMLOutputFactory(ctx); if (encoding == null) encoding = "UTF-8"; try { XMLStreamWriter writer = factory.createXMLStreamWriter(out, encoding); return writer; } catch (XMLStreamException e) { throw new XFireRuntimeException("Couldn't parse stream.", e); } } /** * @return */ public static XMLOutputFactory getXMLOutputFactory(MessageContext ctx) { if (ctx == null) return xmlOututFactory; Object outFactoryObj = ctx.getContextualProperty(XFire.STAX_OUTPUT_FACTORY); if (outFactoryObj instanceof XMLOutputFactory) { return (XMLOutputFactory) outFactoryObj; } else if (outFactoryObj instanceof String) { String outFactory = (String) outFactoryObj; XMLOutputFactory xof = (XMLOutputFactory) factories.get(outFactory); if (xof == null) { xof = (XMLOutputFactory) createFactory(outFactory, ctx); factories.put(outFactory, xof); } return xof; } return xmlOututFactory; } /** * @return */ public static XMLInputFactory getXMLInputFactory(MessageContext ctx) { if (ctx == null) return xmlInputFactory; Object inFactoryObj = ctx.getContextualProperty(XFire.STAX_INPUT_FACTORY); if (inFactoryObj instanceof XMLInputFactory) { return (XMLInputFactory) inFactoryObj; } else if (inFactoryObj instanceof String) { String inFactory = (String) inFactoryObj; XMLInputFactory xif = (XMLInputFactory) factories.get(inFactory); if (xif == null) { xif = (XMLInputFactory) createFactory(inFactory, ctx); configureFactory(xif,ctx); factories.put(inFactory, xif); } return xif; } if(!inFactoryConfigured){ configureFactory(xmlInputFactory,ctx); inFactoryConfigured=true; } return xmlInputFactory; } private static Boolean getBooleanProperty(MessageContext ctx, String name) { Object value = ctx.getContextualProperty(name); if (value != null) { return Boolean.valueOf(value.toString()); } return null; } /** * @param xif * @param ctx */ private static void configureFactory(XMLInputFactory xif, MessageContext ctx) { Boolean value = getBooleanProperty(ctx, XMLInputFactory.IS_VALIDATING); if (value != null) { xif.setProperty(XMLInputFactory.IS_VALIDATING, value); } value = getBooleanProperty(ctx, XMLInputFactory.IS_NAMESPACE_AWARE); if (value != null) { xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, value); } value = getBooleanProperty(ctx, XMLInputFactory.IS_COALESCING); if (value != null) { xif.setProperty(XMLInputFactory.IS_COALESCING, value); } value = getBooleanProperty(ctx, XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES); if (value != null) { xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, value); } value = getBooleanProperty(ctx, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES); if (value != null) { xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, value); } } /** * @param factoryClass * @return */ private static Object createFactory(String factory, MessageContext ctx) { Class factoryClass = null; try { factoryClass = ClassLoaderUtils.loadClass(factory, ctx.getClass()); return factoryClass.newInstance(); } catch (Exception e) { logger.error("Can't create factory for class : " + factory, e); throw new XFireRuntimeException("Can't create factory for class : " + factory); } } /** * @param in * @param encoding * @param ctx * @return */ public static XMLStreamReader createXMLStreamReader(InputStream in, String encoding, MessageContext ctx) { XMLInputFactory factory = getXMLInputFactory(ctx); if (encoding == null) encoding = "UTF-8"; try { return factory.createXMLStreamReader(in, encoding); } catch (XMLStreamException e) { throw new XFireRuntimeException("Couldn't parse stream.", e); } } public static XMLStreamReader createXMLStreamReader(Reader reader) { return createXMLStreamReader(reader, null); } /** * @param reader * @return */ public static XMLStreamReader createXMLStreamReader(Reader reader, MessageContext context) { XMLInputFactory factory = getXMLInputFactory(context); try { return factory.createXMLStreamReader(reader); } catch (XMLStreamException e) { throw new XFireRuntimeException("Couldn't parse stream.", e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -