xmlutils.java
来自「JAVA 所有包」· Java 代码 · 共 694 行 · 第 1/2 页
JAVA
694 行
+ (npe == null ? "" : npe.getMessage()) + "\""); } /** * Method createDSctx * * @param doc * @param prefix * @param namespace * @return the element. */ public static Element createDSctx(Document doc, String prefix, String namespace) { if ((prefix == null) || (prefix.trim().length() == 0)) { throw new IllegalArgumentException("You must supply a prefix"); } Element ctx = doc.createElementNS(null, "namespaceContext"); ctx.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix.trim(), namespace); return ctx; } /** * Method addReturnToElement * * @param e */ public static void addReturnToElement(Element e) { Document doc = e.getOwnerDocument(); e.appendChild(doc.createTextNode("\n")); } /** * Method convertNodelistToSet * * @param xpathNodeSet * @return the set with the nodelist */ public static Set convertNodelistToSet(NodeList xpathNodeSet) { if (xpathNodeSet == null) { return new HashSet(); } int length = xpathNodeSet.getLength(); Set set = new HashSet(length); for (int i = 0; i < length; i++) { set.add(xpathNodeSet.item(i)); } return set; } /** * This method spreads all namespace attributes in a DOM document to their * children. This is needed because the XML Signature XPath transform * must evaluate the XPath against all nodes in the input, even against * XPath namespace nodes. Through a bug in XalanJ2, the namespace nodes are * not fully visible in the Xalan XPath model, so we have to do this by * hand in DOM spaces so that the nodes become visible in XPath space. * * @param doc * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">Namespace axis resolution is not XPath compliant </A> */ public static void circumventBug2650(Document doc) { Element documentElement = doc.getDocumentElement(); // if the document element has no xmlns definition, we add xmlns="" Attr xmlnsAttr = documentElement.getAttributeNodeNS(Constants.NamespaceSpecNS, "xmlns"); if (xmlnsAttr == null) { documentElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", ""); } XMLUtils.circumventBug2650internal(doc); } /** * This is the work horse for {@link #circumventBug2650}. * * @param node * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">Namespace axis resolution is not XPath compliant </A> */ private static void circumventBug2650internal(Node node) { Node parent=null; Node sibling=null; final String namespaceNs=Constants.NamespaceSpecNS; do { switch (node.getNodeType()) { case Node.ELEMENT_NODE : Element element = (Element) node; if (!element.hasChildNodes()) break; if (element.hasAttributes()) { NamedNodeMap attributes = element.getAttributes(); int attributesLength = attributes.getLength(); for (Node child = element.getFirstChild(); child!=null; child=child.getNextSibling()) { if (child.getNodeType() != Node.ELEMENT_NODE) { continue; } Element childElement = (Element) child; for (int i = 0; i < attributesLength; i++) { Attr currentAttr = (Attr) attributes.item(i); if (!namespaceNs.equals(currentAttr.getNamespaceURI())) continue; if (childElement.hasAttributeNS(namespaceNs, currentAttr.getLocalName())) { continue; } childElement.setAttributeNS(namespaceNs, currentAttr.getName(), currentAttr.getNodeValue()); } } } case Node.ENTITY_REFERENCE_NODE : case Node.DOCUMENT_NODE : parent=node; sibling=node.getFirstChild(); break; } while ((sibling==null) && (parent!=null)) { sibling=parent.getNextSibling(); parent=parent.getParentNode(); }; if (sibling==null) { return; } node=sibling; sibling=node.getNextSibling(); } while (true); } /** * @param sibling * @param nodeName * @param number * @return nodes with the constrain */ public static Element selectDsNode(Node sibling, String nodeName, int number) { while (sibling!=null) { if (nodeName.equals(sibling.getLocalName()) && Constants.SignatureSpecNS.equals(sibling.getNamespaceURI())) { if (number==0){ return (Element)sibling; } number--; } sibling=sibling.getNextSibling(); } return null; } /** * @param sibling * @param nodeName * @param number * @return nodes with the constrain */ public static Element selectXencNode(Node sibling, String nodeName, int number) { while (sibling!=null) { if (nodeName.equals(sibling.getLocalName()) && EncryptionConstants.EncryptionSpecNS.equals(sibling.getNamespaceURI())) { if (number==0){ return (Element)sibling; } number--; } sibling=sibling.getNextSibling(); } return null; } /** * @param sibling * @param nodeName * @param number * @return nodes with the constrain */ public static Text selectDsNodeText(Node sibling, String nodeName, int number) { Node n=selectDsNode(sibling,nodeName,number); if (n==null) { return null; } n=n.getFirstChild(); while (n!=null && n.getNodeType()!=Node.TEXT_NODE) { n=n.getNextSibling(); } return (Text)n; } /** * @param sibling * @param uri * @param nodeName * @param number * @return nodes with the constrain */ public static Text selectNodeText(Node sibling, String uri, String nodeName, int number) { Node n=selectNode(sibling,uri,nodeName,number); if (n==null) { return null; } n=n.getFirstChild(); while (n!=null && n.getNodeType()!=Node.TEXT_NODE) { n=n.getNextSibling(); } return (Text)n; } /** * @param sibling * @param uri * @param nodeName * @param number * @return nodes with the constrain */ public static Element selectNode(Node sibling, String uri,String nodeName, int number) { while (sibling!=null) { if (nodeName.equals(sibling.getLocalName()) && uri.equals(sibling.getNamespaceURI())) { if (number==0){ return (Element)sibling; } number--; } sibling=sibling.getNextSibling(); } return null; } /** * @param sibling * @param nodeName * @return nodes with the constrain */ public static Element[] selectDsNodes(Node sibling,String nodeName) { return selectNodes(sibling,Constants.SignatureSpecNS,nodeName); } /** * @param sibling * @param uri * @param nodeName * @return nodes with the constrain */ public static Element[] selectNodes(Node sibling,String uri,String nodeName) { int size=20; Element[] a= new Element[size]; int curr=0; //List list=new ArrayList(); while (sibling!=null) { if (nodeName.equals(sibling.getLocalName()) && uri.equals(sibling.getNamespaceURI())) { a[curr++]=(Element)sibling; if (size<=curr) { int cursize= size<<2; Element []cp=new Element[cursize]; System.arraycopy(a,0,cp,0,size); a=cp; size=cursize; } } sibling=sibling.getNextSibling(); } Element []af=new Element[curr]; System.arraycopy(a,0,af,0,curr); return af; } /** * @param signatureElement * @param inputSet * @return nodes with the constrain */ public static Set excludeNodeFromSet(Node signatureElement, Set inputSet) { Set resultSet = new HashSet(); Iterator iterator = inputSet.iterator(); while (iterator.hasNext()) { Node inputNode = (Node) iterator.next(); if (!XMLUtils .isDescendantOrSelf(signatureElement, inputNode)) { resultSet.add(inputNode); } } return resultSet; } /** * Returns true if the descendantOrSelf is on the descendant-or-self axis * of the context node. * * @param ctx * @param descendantOrSelf * @return true if the node is descendant */ static public boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) { if (ctx == descendantOrSelf) { return true; } Node parent = descendantOrSelf; while (true) { if (parent == null) { return false; } if (parent == ctx) { return true; } if (parent.getNodeType() == Node.ATTRIBUTE_NODE) { parent = ((Attr) parent).getOwnerElement(); } else { parent = parent.getParentNode(); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?