一、前言
关于jackrabbit源码包中的org.apache.jackrabbit.webdav.xml.DomUtil文档节点工具类,对文档Document、属性Attr、元素Element集合及Namespace命名空间等解析处理,详情参见源码部分。
二、源码部分
1.DomUtil工具类
package org.apache.jackrabbit.webdav.xml;@b@@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.io.Writer;@b@import java.util.ArrayList;@b@import java.util.List;@b@import javax.xml.namespace.QName;@b@import javax.xml.parsers.DocumentBuilder;@b@import javax.xml.parsers.DocumentBuilderFactory;@b@import javax.xml.parsers.ParserConfigurationException;@b@import javax.xml.transform.Transformer;@b@import javax.xml.transform.TransformerException;@b@import javax.xml.transform.TransformerFactory;@b@import javax.xml.transform.dom.DOMSource;@b@import javax.xml.transform.stream.StreamResult;@b@import org.apache.jackrabbit.webdav.DavConstants;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@import org.w3c.dom.Attr;@b@import org.w3c.dom.CharacterData;@b@import org.w3c.dom.Document;@b@import org.w3c.dom.Element;@b@import org.w3c.dom.NamedNodeMap;@b@import org.w3c.dom.Node;@b@import org.w3c.dom.NodeList;@b@import org.w3c.dom.Text;@b@import org.xml.sax.SAXException;@b@import org.xml.sax.helpers.DefaultHandler;@b@@b@public class DomUtil@b@{@b@ private static Logger log = LoggerFactory.getLogger(DomUtil.class);@b@ private static DocumentBuilderFactory BUILDER_FACTORY = createFactory();@b@ private static TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();@b@@b@ private static DocumentBuilderFactory createFactory()@b@ {@b@ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();@b@ factory.setNamespaceAware(true);@b@ factory.setIgnoringComments(true);@b@ factory.setIgnoringElementContentWhitespace(true);@b@ factory.setCoalescing(true);@b@ try {@b@ factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);@b@ } catch (ParserConfigurationException e) {@b@ log.warn("Secure XML processing is not supported", e);@b@ } catch (AbstractMethodError e) {@b@ log.warn("Secure XML processing is not supported", e);@b@ }@b@ return factory;@b@ }@b@@b@ public static void setBuilderFactory(DocumentBuilderFactory documentBuilderFactory)@b@ {@b@ BUILDER_FACTORY = documentBuilderFactory;@b@ }@b@@b@ public static Document createDocument()@b@ throws ParserConfigurationException@b@ {@b@ return BUILDER_FACTORY.newDocumentBuilder().newDocument();@b@ }@b@@b@ public static Document parseDocument(InputStream stream)@b@ throws ParserConfigurationException, SAXException, IOException@b@ {@b@ DocumentBuilder docBuilder = BUILDER_FACTORY.newDocumentBuilder();@b@@b@ docBuilder.setErrorHandler(new DefaultHandler());@b@@b@ return docBuilder.parse(stream);@b@ }@b@@b@ public static String getAttribute(Element parent, String localName, Namespace namespace)@b@ {@b@ Attr attribute;@b@ if (parent == null) {@b@ return null;@b@ }@b@@b@ if (namespace == null)@b@ attribute = parent.getAttributeNode(localName);@b@ else@b@ attribute = parent.getAttributeNodeNS(namespace.getURI(), localName);@b@@b@ if (attribute != null)@b@ return attribute.getValue();@b@@b@ return null;@b@ }@b@@b@ public static Attr[] getNamespaceAttributes(Element element)@b@ {@b@ NamedNodeMap attributes = element.getAttributes();@b@ List nsAttr = new ArrayList();@b@ for (int i = 0; i < attributes.getLength(); ++i) {@b@ Attr attr = (Attr)attributes.item(i);@b@ if (Namespace.XMLNS_NAMESPACE.getURI().equals(attr.getNamespaceURI()))@b@ nsAttr.add(attr);@b@ }@b@@b@ return ((Attr[])nsAttr.toArray(new Attr[nsAttr.size()]));@b@ }@b@@b@ public static String getText(Element element)@b@ {@b@ NodeList nodes;@b@ int i;@b@ StringBuffer content = new StringBuffer();@b@ if (element != null) {@b@ nodes = element.getChildNodes();@b@ for (i = 0; i < nodes.getLength(); ++i) {@b@ Node child = nodes.item(i);@b@ if (isText(child))@b@ {@b@ content.append(((CharacterData)child).getData());@b@ }@b@ }@b@ }@b@ return ((content.length() == 0) ? null : content.toString());@b@ }@b@@b@ public static String getText(Element element, String defaultValue)@b@ {@b@ String txt = getText(element);@b@ return ((txt == null) ? defaultValue : txt);@b@ }@b@@b@ public static String getTextTrim(Element element)@b@ {@b@ String txt = getText(element);@b@ return ((txt == null) ? txt : txt.trim());@b@ }@b@@b@ public static String getChildText(Element parent, String childLocalName, Namespace childNamespace)@b@ {@b@ Element child = getChildElement(parent, childLocalName, childNamespace);@b@ return ((child == null) ? null : getText(child));@b@ }@b@@b@ public static String getChildTextTrim(Element parent, String childLocalName, Namespace childNamespace)@b@ {@b@ Element child = getChildElement(parent, childLocalName, childNamespace);@b@ return ((child == null) ? null : getTextTrim(child));@b@ }@b@@b@ public static String getChildTextTrim(Element parent, QName childName)@b@ {@b@ Element child = getChildElement(parent, childName);@b@ return ((child == null) ? null : getTextTrim(child));@b@ }@b@@b@ public static boolean hasChildElement(Node parent, String childLocalName, Namespace childNamespace)@b@ {@b@ return (getChildElement(parent, childLocalName, childNamespace) != null);@b@ }@b@@b@ public static Element getChildElement(Node parent, String childLocalName, Namespace childNamespace)@b@ {@b@ NodeList children;@b@ int i;@b@ if (parent != null) {@b@ children = parent.getChildNodes();@b@ for (i = 0; i < children.getLength(); ++i) {@b@ Node child = children.item(i);@b@ if ((isElement(child)) && (matches(child, childLocalName, childNamespace)))@b@ return ((Element)child);@b@ }@b@ }@b@@b@ return null;@b@ }@b@@b@ public static Element getChildElement(Node parent, QName childName)@b@ {@b@ NodeList children;@b@ int i;@b@ if (parent != null) {@b@ children = parent.getChildNodes();@b@ for (i = 0; i < children.getLength(); ++i) {@b@ Node child = children.item(i);@b@ if ((isElement(child)) && (matches(child, childName)))@b@ return ((Element)child);@b@ }@b@ }@b@@b@ return null;@b@ }@b@@b@ public static ElementIterator getChildren(Element parent, String childLocalName, Namespace childNamespace)@b@ {@b@ return new ElementIterator(parent, childLocalName, childNamespace);@b@ }@b@@b@ public static ElementIterator getChildren(Element parent, QName childName)@b@ {@b@ return new ElementIterator(parent, childName);@b@ }@b@@b@ public static ElementIterator getChildren(Element parent)@b@ {@b@ return new ElementIterator(parent);@b@ }@b@@b@ public static Element getFirstChildElement(Node parent)@b@ {@b@ NodeList children;@b@ int i;@b@ if (parent != null) {@b@ children = parent.getChildNodes();@b@ for (i = 0; i < children.getLength(); ++i) {@b@ Node child = children.item(i);@b@ if (isElement(child))@b@ return ((Element)child);@b@ }@b@ }@b@@b@ return null;@b@ }@b@@b@ public static boolean hasContent(Node parent)@b@ {@b@ NodeList children;@b@ int i;@b@ if (parent != null) {@b@ children = parent.getChildNodes();@b@ for (i = 0; i < children.getLength(); ++i) {@b@ Node child = children.item(i);@b@ if (isAcceptedNode(child))@b@ return true;@b@ }@b@ }@b@@b@ return false;@b@ }@b@@b@ public static List<Node> getContent(Node parent)@b@ {@b@ NodeList children;@b@ int i;@b@ List content = new ArrayList();@b@ if (parent != null) {@b@ children = parent.getChildNodes();@b@ for (i = 0; i < children.getLength(); ++i) {@b@ Node child = children.item(i);@b@ if (isAcceptedNode(child))@b@ content.add(child);@b@ }@b@ }@b@@b@ return content;@b@ }@b@@b@ public static Namespace getNamespace(Element element)@b@ {@b@ String uri = element.getNamespaceURI();@b@ String prefix = element.getPrefix();@b@ if (uri == null)@b@ return Namespace.EMPTY_NAMESPACE;@b@@b@ return Namespace.getNamespace(prefix, uri);@b@ }@b@@b@ public static boolean matches(Node node, String requiredLocalName, Namespace requiredNamespace)@b@ {@b@ if (node == null)@b@ return false;@b@@b@ boolean matchingNamespace = matchingNamespace(node, requiredNamespace);@b@ return ((matchingNamespace) && (matchingLocalName(node, requiredLocalName)));@b@ }@b@@b@ public static boolean matches(Node node, QName requiredName)@b@ {@b@ if (node == null)@b@ return false;@b@@b@ String nodens = (node.getNamespaceURI() != null) ? node.getNamespaceURI() : "";@b@ return ((nodens.equals(requiredName.getNamespaceURI())) && (node.getLocalName().equals(requiredName.getLocalPart())));@b@ }@b@@b@ private static boolean matchingNamespace(Node node, Namespace requiredNamespace)@b@ {@b@ if (requiredNamespace == null)@b@ return true;@b@@b@ return requiredNamespace.isSame(node.getNamespaceURI());@b@ }@b@@b@ private static boolean matchingLocalName(Node node, String requiredLocalName)@b@ {@b@ if (requiredLocalName == null)@b@ return true;@b@@b@ String localName = node.getLocalName();@b@ return requiredLocalName.equals(localName);@b@ }@b@@b@ private static boolean isAcceptedNode(Node node)@b@ {@b@ return ((isElement(node)) || (isText(node)));@b@ }@b@@b@ static boolean isElement(Node node)@b@ {@b@ return (node.getNodeType() == 1);@b@ }@b@@b@ static boolean isText(Node node)@b@ {@b@ int ntype = node.getNodeType();@b@ return ((ntype == 3) || (ntype == 4));@b@ }@b@@b@ public static Element createElement(Document factory, String localName, Namespace namespace)@b@ {@b@ if (namespace != null)@b@ return factory.createElementNS(namespace.getURI(), getPrefixedName(localName, namespace));@b@@b@ return factory.createElement(localName);@b@ }@b@@b@ public static Element createElement(Document factory, String localName, Namespace namespace, String text)@b@ {@b@ Element elem = createElement(factory, localName, namespace);@b@ setText(elem, text);@b@ return elem;@b@ }@b@@b@ public static Element addChildElement(Element parent, String localName, Namespace namespace)@b@ {@b@ Element elem = createElement(parent.getOwnerDocument(), localName, namespace);@b@ parent.appendChild(elem);@b@ return elem;@b@ }@b@@b@ public static Element addChildElement(Node parent, String localName, Namespace namespace)@b@ {@b@ Document doc = parent.getOwnerDocument();@b@ if (parent instanceof Document)@b@ doc = (Document)parent;@b@@b@ Element elem = createElement(doc, localName, namespace);@b@ parent.appendChild(elem);@b@ return elem;@b@ }@b@@b@ public static Element addChildElement(Element parent, String localName, Namespace namespace, String text)@b@ {@b@ Element elem = createElement(parent.getOwnerDocument(), localName, namespace, text);@b@ parent.appendChild(elem);@b@ return elem;@b@ }@b@@b@ public static void setText(Element element, String text)@b@ {@b@ if ((text == null) || ("".equals(text)))@b@ {@b@ return;@b@ }@b@ Text txt = element.getOwnerDocument().createTextNode(text);@b@ element.appendChild(txt);@b@ }@b@@b@ public static void setAttribute(Element element, String attrLocalName, Namespace attrNamespace, String attrValue)@b@ {@b@ Attr attr;@b@ if (attrNamespace == null) {@b@ attr = element.getOwnerDocument().createAttribute(attrLocalName);@b@ attr.setValue(attrValue);@b@ element.setAttributeNode(attr);@b@ } else {@b@ attr = element.getOwnerDocument().createAttributeNS(attrNamespace.getURI(), getPrefixedName(attrLocalName, attrNamespace));@b@ attr.setValue(attrValue);@b@ element.setAttributeNodeNS(attr);@b@ }@b@ }@b@@b@ public static void setNamespaceAttribute(Element element, String prefix, String uri)@b@ {@b@ if (Namespace.EMPTY_NAMESPACE.equals(Namespace.getNamespace(prefix, uri)))@b@ {@b@ log.debug("Empty namespace -> omit attribute setting.");@b@ return;@b@ }@b@ setAttribute(element, prefix, Namespace.XMLNS_NAMESPACE, uri);@b@ }@b@@b@ public static Element timeoutToXml(long timeout, Document factory)@b@ {@b@ boolean infinite = (timeout / 1000L > 2147483647L) || (timeout == 2147483647L);@b@ String expString = "Second-" + (timeout / 1000L);@b@ return createElement(factory, "timeout", DavConstants.NAMESPACE, expString);@b@ }@b@@b@ public static Element depthToXml(boolean isDeep, Document factory)@b@ {@b@ return depthToXml((isDeep) ? "infinity" : "0", factory);@b@ }@b@@b@ public static Element depthToXml(String depth, Document factory)@b@ {@b@ return createElement(factory, "depth", DavConstants.NAMESPACE, depth);@b@ }@b@@b@ public static Element hrefToXml(String href, Document factory)@b@ {@b@ return createElement(factory, "href", DavConstants.NAMESPACE, href);@b@ }@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static String getQualifiedName(String localName, Namespace namespace)@b@ {@b@ return getExpandedName(localName, namespace);@b@ }@b@@b@ public static String getExpandedName(String localName, Namespace namespace)@b@ {@b@ if ((namespace == null) || (namespace.equals(Namespace.EMPTY_NAMESPACE)))@b@ return localName;@b@@b@ StringBuffer b = new StringBuffer("{");@b@ b.append(namespace.getURI()).append("}");@b@ b.append(localName);@b@ return b.toString();@b@ }@b@@b@ public static String getPrefixedName(String localName, Namespace namespace)@b@ {@b@ if ((namespace == null) || (Namespace.EMPTY_NAMESPACE.equals(namespace)) || (Namespace.EMPTY_NAMESPACE.getPrefix().equals(namespace.getPrefix())))@b@ {@b@ return localName;@b@ }@b@ StringBuffer buf = new StringBuffer(namespace.getPrefix());@b@ buf.append(":");@b@ buf.append(localName);@b@ return buf.toString();@b@ }@b@@b@ public static void transformDocument(Document xmlDoc, Writer writer)@b@ throws TransformerException, SAXException@b@ {@b@ Transformer transformer = TRANSFORMER_FACTORY.newTransformer();@b@ transformer.transform(new DOMSource(xmlDoc), ResultHelper.getResult(new StreamResult(writer)));@b@ }@b@@b@ public static void transformDocument(Document xmlDoc, OutputStream out)@b@ throws TransformerException, SAXException@b@ {@b@ Transformer transformer = TRANSFORMER_FACTORY.newTransformer();@b@ transformer.transform(new DOMSource(xmlDoc), ResultHelper.getResult(new StreamResult(out)));@b@ }@b@}
2.其他依赖类-Namespace、ElementIterator、ResultHelper等
package org.apache.jackrabbit.webdav.xml;@b@@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class Namespace@b@{@b@ private static Logger log = LoggerFactory.getLogger(Namespace.class);@b@ public static final Namespace EMPTY_NAMESPACE = new Namespace("", "");@b@ public static final Namespace XML_NAMESPACE = new Namespace("xml", "http://www.w3.org/XML/1998/namespace");@b@ public static final Namespace XMLNS_NAMESPACE = new Namespace("xmlns", "http://www.w3.org/2000/xmlns/");@b@ private final String prefix;@b@ private final String uri;@b@@b@ private Namespace(String prefix, String uri)@b@ {@b@ this.prefix = prefix;@b@ this.uri = uri;@b@ }@b@@b@ public static Namespace getNamespace(String prefix, String uri)@b@ {@b@ if (prefix == null)@b@ prefix = EMPTY_NAMESPACE.getPrefix();@b@@b@ if (uri == null)@b@ uri = EMPTY_NAMESPACE.getURI();@b@@b@ return new Namespace(prefix, uri);@b@ }@b@@b@ public static Namespace getNamespace(String uri) {@b@ return getNamespace("", uri);@b@ }@b@@b@ public String getPrefix()@b@ {@b@ return this.prefix;@b@ }@b@@b@ public String getURI() {@b@ return this.uri;@b@ }@b@@b@ public boolean isSame(String namespaceURI)@b@ {@b@ Namespace other = getNamespace(namespaceURI);@b@ return equals(other);@b@ }@b@@b@ public int hashCode()@b@ {@b@ return this.uri.hashCode();@b@ }@b@@b@ public boolean equals(Object obj)@b@ {@b@ if (obj == this)@b@ return true;@b@@b@ if (obj instanceof Namespace)@b@ return this.uri.equals(((Namespace)obj).uri);@b@@b@ return false;@b@ }@b@}
package org.apache.jackrabbit.webdav.xml;@b@@b@import java.util.Iterator;@b@import java.util.NoSuchElementException;@b@import javax.xml.namespace.QName;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@import org.w3c.dom.Element;@b@import org.w3c.dom.Node;@b@import org.w3c.dom.NodeList;@b@@b@public class ElementIterator@b@ implements Iterator<Element>@b@{@b@ private static Logger log = LoggerFactory.getLogger(ElementIterator.class);@b@ private final Namespace namespace;@b@ private final String localName;@b@ private final QName qName;@b@ private Element next;@b@@b@ public ElementIterator(Element parent, String localName, Namespace namespace)@b@ {@b@ this.localName = localName;@b@ this.namespace = namespace;@b@ this.qName = null;@b@ seek(parent);@b@ }@b@@b@ public ElementIterator(Element parent, QName qname)@b@ {@b@ this.localName = null;@b@ this.namespace = null;@b@ this.qName = qname;@b@ seek(parent);@b@ }@b@@b@ public ElementIterator(Element parent)@b@ {@b@ this(parent, null, null);@b@ }@b@@b@ public void remove()@b@ {@b@ throw new UnsupportedOperationException("Remove not implemented.");@b@ }@b@@b@ public boolean hasNext()@b@ {@b@ return (this.next != null);@b@ }@b@@b@ public Element next()@b@ {@b@ return nextElement();@b@ }@b@@b@ public Element nextElement()@b@ {@b@ if (this.next == null)@b@ throw new NoSuchElementException();@b@@b@ Element ret = this.next;@b@ seek();@b@ return ret;@b@ }@b@@b@ private void seek(Element parent)@b@ {@b@ NodeList nodeList = parent.getChildNodes();@b@ for (int i = 0; i < nodeList.getLength(); ++i) {@b@ Node n = nodeList.item(i);@b@ if (matchesName(n)) {@b@ this.next = ((Element)n);@b@ return;@b@ }@b@ }@b@ }@b@@b@ private void seek()@b@ {@b@ Node n = this.next.getNextSibling();@b@ while (n != null) {@b@ if (matchesName(n)) {@b@ this.next = ((Element)n);@b@ return;@b@ }@b@ n = n.getNextSibling();@b@ }@b@@b@ this.next = null;@b@ }@b@@b@ private boolean matchesName(Node n)@b@ {@b@ if (!(DomUtil.isElement(n)))@b@ return false;@b@ if (this.qName != null)@b@ return DomUtil.matches(n, this.qName);@b@@b@ return DomUtil.matches(n, this.localName, this.namespace);@b@ }@b@}
package org.apache.jackrabbit.webdav.xml;@b@@b@import java.io.StringWriter;@b@import java.util.ArrayList;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Map;@b@import javax.xml.transform.Result;@b@import javax.xml.transform.Transformer;@b@import javax.xml.transform.TransformerConfigurationException;@b@import javax.xml.transform.TransformerFactory;@b@import javax.xml.transform.sax.SAXResult;@b@import javax.xml.transform.sax.SAXTransformerFactory;@b@import javax.xml.transform.sax.TransformerHandler;@b@import javax.xml.transform.stream.StreamResult;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@import org.xml.sax.Attributes;@b@import org.xml.sax.ContentHandler;@b@import org.xml.sax.Locator;@b@import org.xml.sax.SAXException;@b@import org.xml.sax.helpers.AttributesImpl;@b@import org.xml.sax.helpers.DefaultHandler;@b@@b@public final class ResultHelper@b@{@b@ private static final Logger log = LoggerFactory.getLogger(ResultHelper.class);@b@ private static final String XML = "http://www.w3.org/XML/1998/namespace";@b@ private static final SAXTransformerFactory FACTORY = (SAXTransformerFactory)TransformerFactory.newInstance();@b@ private static final boolean NEEDS_XMLNS_ATTRIBUTES = needsXmlnsAttributes();@b@@b@ private static boolean needsXmlnsAttributes()@b@ {@b@ StringWriter writer;@b@ try@b@ {@b@ writer = new StringWriter();@b@ TransformerHandler probe = FACTORY.newTransformerHandler();@b@ probe.setResult(new StreamResult(writer));@b@ probe.startDocument();@b@ probe.startPrefixMapping("p", "uri");@b@ probe.startElement("uri", "e", "p:e", new AttributesImpl());@b@ probe.endElement("uri", "e", "p:e");@b@ probe.endPrefixMapping("p");@b@ probe.endDocument();@b@ return (writer.toString().indexOf("xmlns") == -1);@b@ } catch (Exception e) {@b@ throw new UnsupportedOperationException("XML serialization fails");@b@ }@b@ }@b@@b@ public static Result getResult(Result result)@b@ throws SAXException@b@ {@b@ TransformerHandler handler;@b@ try@b@ {@b@ handler = FACTORY.newTransformerHandler();@b@ handler.setResult(result);@b@@b@ Transformer transformer = handler.getTransformer();@b@ transformer.setOutputProperty("method", "xml");@b@ transformer.setOutputProperty("encoding", "UTF-8");@b@ transformer.setOutputProperty("indent", "no");@b@@b@ if (NEEDS_XMLNS_ATTRIBUTES)@b@ {@b@ return new SAXResult(new SerializingContentHandler(handler, null));@b@ }@b@ return result;@b@ }@b@ catch (TransformerConfigurationException e) {@b@ throw new SAXException("Failed to initialize XML serializer", e);@b@ }@b@ }@b@@b@ private static final class SerializingContentHandler extends DefaultHandler@b@ {@b@ private List prefixList = new ArrayList();@b@ private List uriList = new ArrayList();@b@ private Map uriToPrefixMap = new HashMap();@b@ private Map prefixToUriMap = new HashMap();@b@ private boolean hasMappings = false;@b@ private final List addedPrefixMappings = new ArrayList();@b@ private final ContentHandler handler;@b@@b@ private SerializingContentHandler(ContentHandler handler)@b@ {@b@ this.handler = handler;@b@ }@b@@b@ public void characters(char[] ch, int start, int length) throws SAXException@b@ {@b@ this.handler.characters(ch, start, length);@b@ }@b@@b@ public void startDocument()@b@ throws SAXException@b@ {@b@ this.uriToPrefixMap.clear();@b@ this.prefixToUriMap.clear();@b@ clearMappings();@b@@b@ this.handler.startDocument();@b@ }@b@@b@ public void startPrefixMapping(String prefix, String uri)@b@ throws SAXException@b@ {@b@ if ((uri != null) && (!(prefix.startsWith("xml")))) {@b@ this.hasMappings = true;@b@ this.prefixList.add(prefix);@b@ this.uriList.add(uri);@b@@b@ if (prefix.length() > 0)@b@ this.uriToPrefixMap.put(uri, prefix + ":");@b@ else {@b@ this.uriToPrefixMap.put(uri, prefix);@b@ }@b@@b@ this.prefixToUriMap.put(prefix, uri);@b@ }@b@@b@ this.handler.startPrefixMapping(prefix, uri);@b@ }@b@@b@ private void checkPrefixMapping(String uri, String qname)@b@ throws SAXException@b@ {@b@ if ((uri != null) && (uri.length() > 0) && (!(uri.startsWith("xml"))) && (!(this.uriToPrefixMap.containsKey(uri))))@b@ {@b@ String prefix = "ns";@b@ if ((qname != null) && (qname.length() > 0)) {@b@ int colon = qname.indexOf(58);@b@ if (colon != -1) {@b@ prefix = qname.substring(0, colon);@b@ }@b@@b@ }@b@@b@ String base = prefix;@b@ for (int i = 2; this.prefixToUriMap.containsKey(prefix); ++i) {@b@ prefix = base + i;@b@ }@b@@b@ int last = this.addedPrefixMappings.size() - 1;@b@ List prefixes = (List)this.addedPrefixMappings.get(last);@b@ if (prefixes == null) {@b@ prefixes = new ArrayList();@b@ this.addedPrefixMappings.set(last, prefixes);@b@ }@b@ prefixes.add(prefix);@b@@b@ startPrefixMapping(prefix, uri);@b@ }@b@ }@b@@b@ public void startElement(String eltUri, String eltLocalName, String eltQName, Attributes attrs)@b@ throws SAXException@b@ {@b@ this.addedPrefixMappings.add(null);@b@ checkPrefixMapping(eltUri, eltQName);@b@ for (int i = 0; i < attrs.getLength(); ++i) {@b@ checkPrefixMapping(attrs.getURI(i), attrs.getQName(i));@b@ }@b@@b@ if ((null != eltUri) && (eltUri.length() != 0) && (this.uriToPrefixMap.containsKey(eltUri)))@b@ eltQName = this.uriToPrefixMap.get(eltUri) + eltLocalName;@b@@b@ if (this.hasMappings)@b@ {@b@ AttributesImpl newAttrs = null;@b@@b@ int mappingCount = this.prefixList.size();@b@ int attrCount = attrs.getLength();@b@@b@ for (int mapping = 0; mapping < mappingCount; ++mapping)@b@ {@b@ String uri = (String)this.uriList.get(mapping);@b@ String prefix = (String)this.prefixList.get(mapping);@b@ String qName = "xmlns:" + prefix;@b@@b@ boolean found = false;@b@ for (int attr = 0; attr < attrCount; ++attr)@b@ if (qName.equals(attrs.getQName(attr)))@b@ {@b@ if (!(uri.equals(attrs.getValue(attr))))@b@ throw new SAXException("URI in prefix mapping and attribute do not match");@b@@b@ found = true;@b@ break;@b@ }@b@@b@@b@ if (!(found))@b@ {@b@ if (newAttrs == null)@b@ {@b@ if (attrCount == 0)@b@ newAttrs = new AttributesImpl();@b@ else@b@ newAttrs = new AttributesImpl(attrs);@b@@b@ }@b@@b@ if (prefix.equals(""))@b@ newAttrs.addAttribute("http://www.w3.org/XML/1998/namespace", qName, qName, "CDATA", uri);@b@ else@b@ newAttrs.addAttribute("http://www.w3.org/XML/1998/namespace", prefix, qName, "CDATA", uri);@b@@b@ }@b@@b@ }@b@@b@ clearMappings();@b@@b@ this.handler.startElement(eltUri, eltLocalName, eltQName, (newAttrs == null) ? attrs : newAttrs);@b@ }@b@ else {@b@ this.handler.startElement(eltUri, eltLocalName, eltQName, attrs);@b@ }@b@ }@b@@b@ public void endElement(String eltUri, String eltLocalName, String eltQName)@b@ throws SAXException@b@ {@b@ if ((null != eltUri) && (eltUri.length() != 0) && (this.uriToPrefixMap.containsKey(eltUri))) {@b@ eltQName = this.uriToPrefixMap.get(eltUri) + eltLocalName;@b@ }@b@@b@ this.handler.endElement(eltUri, eltLocalName, eltQName);@b@@b@ int last = this.addedPrefixMappings.size() - 1;@b@ List prefixes = (List)this.addedPrefixMappings.remove(last);@b@ if (prefixes != null) {@b@ Iterator iterator = prefixes.iterator();@b@ while (iterator.hasNext())@b@ endPrefixMapping((String)iterator.next());@b@ }@b@ }@b@@b@ public void endPrefixMapping(String prefix)@b@ throws SAXException@b@ {@b@ if (this.prefixToUriMap.containsKey(prefix)) {@b@ this.uriToPrefixMap.remove(this.prefixToUriMap.get(prefix));@b@ this.prefixToUriMap.remove(prefix);@b@ }@b@@b@ if (this.hasMappings)@b@ {@b@ int pos = this.prefixList.lastIndexOf(prefix);@b@ if (pos != -1) {@b@ this.prefixList.remove(pos);@b@ this.uriList.remove(pos);@b@ }@b@ }@b@@b@ this.handler.endPrefixMapping(prefix);@b@ }@b@@b@ public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException@b@ {@b@ this.handler.ignorableWhitespace(ch, start, length);@b@ }@b@@b@ public void processingInstruction(String target, String data) throws SAXException@b@ {@b@ this.handler.processingInstruction(target, data);@b@ }@b@@b@ public void setDocumentLocator(Locator locator)@b@ {@b@ this.handler.setDocumentLocator(locator);@b@ }@b@@b@ public void skippedEntity(String name) throws SAXException@b@ {@b@ this.handler.skippedEntity(name);@b@ }@b@@b@ public void endDocument()@b@ throws SAXException@b@ {@b@ this.uriToPrefixMap.clear();@b@ this.prefixToUriMap.clear();@b@ clearMappings();@b@@b@ this.handler.endDocument();@b@ }@b@@b@ private void clearMappings() {@b@ this.hasMappings = false;@b@ this.prefixList.clear();@b@ this.uriList.clear();@b@ }@b@ }@b@}