📄 utils.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.axis.wsdl.symbolTable;import org.apache.axis.Constants;import org.apache.axis.utils.XMLUtils;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import javax.xml.namespace.QName;import javax.xml.rpc.holders.BooleanHolder;import java.util.*;/** * This class contains static utility methods for the emitter. * * @author Rich Scheuerle (scheu@us.ibm.com) * @author Tom Jordahl (tomj@macromedia.com) */public class Utils { /** cache of namespaces -> maps of localNames -> QNames */ static final Map nsmap = new HashMap(); /** * Find or create a QName with the specified namespace/localName. * * @param namespace * @param localName * @return */ static QName findQName(String namespace, String localName) { QName qname = null; // get the inner map, using the namespace as a key Map ln2qn = (Map) nsmap.get(namespace); if (null == ln2qn) { // cache miss ln2qn = new HashMap(); nsmap.put(namespace, ln2qn); qname = new QName(namespace, localName); ln2qn.put(localName, qname); } else { // cache hit qname = (QName) ln2qn.get(localName); if (null == qname) { // cache miss qname = new QName(namespace, localName); ln2qn.put(localName, qname); } else { // cache hit } } return qname; } /** * Given a node, return the value of the given attribute. * If the attribute does not exist, searching continues through ancestor nodes until found. * This method is useful for finding attributes that pertain to a group of contained * nodes (i.e. xlmns, xmlns:tns, targetNamespace, name) * * @param node * @param attr * @return */ public static String getScopedAttribute(Node node, String attr) { if (node == null) { return null; } if (node.getAttributes() == null) { return getScopedAttribute(node.getParentNode(), attr); } Node attrNode = node.getAttributes().getNamedItem(attr); if (attrNode != null) { return attrNode.getNodeValue(); } else { return getScopedAttribute(node.getParentNode(), attr); } } /** * Given a node, return the value of the given attribute. * Returns null if the attribute is not found * * @param node * @param attr * @return */ public static String getAttribute(Node node, String attr) { if ((node == null) || (node.getAttributes() == null)) { return null; } Node attrNode = node.getAttributes().getNamedItem(attr); if (attrNode != null) { return attrNode.getNodeValue(); } else { return null; } } /** * Given a node, return the attributes that have the specified local name. * Returns null if the attribute is not found * * @param node * @param localName * @return */ public static Vector getAttributesWithLocalName(Node node, String localName) { Vector v = new Vector(); if (node == null) { return v; } NamedNodeMap map = node.getAttributes(); if (map != null) { for (int i = 0; i < map.getLength(); i++) { Node attrNode = map.item(i); if ((attrNode != null) && attrNode.getLocalName().equals(localName)) { v.add(attrNode); } } } return v; } /** * An xml element may have a name. * For example <.element name="foo" type="b:bar">. * has the name "element". This routine gets the full QName of the element. * * @param node * @return */ public static QName getNodeQName(Node node) { if (node == null) { return null; } String localName = node.getLocalName(); if (localName == null) { return null; } String namespace = node.getNamespaceURI(); return (findQName(namespace, localName)); } /** * XML nodes may have a name attribute. * For example <.element name="foo" type="b:bar">. * has the name attribute value "foo". This routine gets the QName of the name attribute value. * * @param node * @return */ public static QName getNodeNameQName(Node node) { if (node == null) { return null; } String localName = null; String namespace = null; // First try to get the name directly localName = getAttribute(node, "name"); // If this fails and the node has a ref, use the ref name. if (localName == null) { QName ref = getTypeQNameFromAttr(node, "ref"); if (ref != null) { localName = ref.getLocalPart(); namespace = ref.getNamespaceURI(); } } // This routine may be called for complexType elements. In some cases, // the complexType may be anonymous, which is why the getScopedAttribute // method is used. Node search = node.getParentNode(); while (search != null) { String ln = search.getLocalName(); if (ln.equals("schema")) { search = null; } else if (ln.equals("element") || ln.equals("attribute")) { localName = SymbolTable.ANON_TOKEN + getNodeNameQName(search).getLocalPart(); search = null; } else if (ln.equals("complexType") || ln.equals("simpleType")) { localName = getNodeNameQName(search).getLocalPart() + SymbolTable.ANON_TOKEN + localName; search = null; } else { search = search.getParentNode(); } } if (localName == null) { return null; } // Build and return the QName if (namespace == null) { namespace = getScopedAttribute(node, "targetNamespace"); } return (findQName(namespace, localName)); } /** * An XML element or attribute node has several ways of * identifying the type of the element or attribute: * - use the type attribute to reference a complexType/simpleType * - use the ref attribute to reference another element, group or attributeGroup * - use of an anonymous type (i.e. a nested type underneath itself) * - a wsdl:part can use the element attribute. * - an extension can use the base attribute. * <p/> * This routine returns a QName representing this "type". * The forElement value is also returned to indicate whether the * QName represents an element (i.e. obtained using the ref attribute) * or a type. * <p/> * Other attributes affect the QName that is returned. * If the "minOccurs" and "maxOccurs" are set such that the * type is a collection of "types", then an artificial qname is * returned to represent the collection. * * @param node of the reference * @param forElement output parameter is set to true if QName is for an element * (i.e. ref= or element= attribute was used). * @param ignoreMaxOccurs indicates whether minOccurs/maxOccurs affects the QName * @return QName representing the type of this element */ public static QName getTypeQName(Node node, BooleanHolder forElement, boolean ignoreMaxOccurs) { if (node == null) { return null; } forElement.value = false; // Assume QName returned is for a type // Try getting the QName of the type attribute. // Note this also retrieves the type if an anonymous type is used. QName qName = getTypeQNameFromAttr(node, "type"); // If not successful, try using the ref attribute. if (qName == null) { String localName = node.getLocalName(); // bug 23145: support attributeGroup (Brook Richan) // a ref can be for an element or attributeGroup if ((localName != null) && !(localName.equals("attributeGroup") || localName.equals("group") || localName.equals("list"))) { forElement.value = true; } qName = getTypeQNameFromAttr(node, "ref"); } // in case of a list get the itemType if (qName == null) { qName = getTypeQNameFromAttr(node, "itemType"); } // If the node has "type"/"ref" and "maxOccurs" then the type is really // a collection. There is no qname in the wsdl which we can use to represent // the collection, so we need to invent one. // The local part of the qname is changed to <local>[minOccurs, maxOccurs] // The namespace uri is changed to the targetNamespace of this node if (!ignoreMaxOccurs) { if (qName != null) { String maxOccursValue = getAttribute(node, "maxOccurs"); String minOccursValue = getAttribute(node, "minOccurs"); String nillableValue = getAttribute(node, "nillable"); if (maxOccursValue == null) { maxOccursValue = "1"; } if (minOccursValue == null) { minOccursValue = "1"; } if (minOccursValue.equals("0") && maxOccursValue.equals("1")) { // If we have a minoccurs="0"/maxoccurs="1", this is just // like a nillable single value, so treat it as such.// qName = getNillableQName(qName); } else if (!maxOccursValue.equals("1") || !minOccursValue.equals("1")) { String localPart = qName.getLocalPart(); String wrapped = (nillableValue != null && nillableValue.equals("true") ? " wrapped" : ""); String range = "["; if (!minOccursValue.equals("1")) { range += minOccursValue; } range += ","; if (!maxOccursValue.equals("1")) { range += maxOccursValue; } range += "]"; localPart += range + wrapped; qName = findQName(qName.getNamespaceURI(), localPart); } } } // A WSDL Part uses the element attribute instead of the ref attribute if (qName == null) { forElement.value = true; qName = getTypeQNameFromAttr(node, "element"); } // "base" references a "type" if (qName == null) { forElement.value = false; qName = getTypeQNameFromAttr(node, "base"); } return qName; } /** * Method getMemberTypeQNames * * @param node * @return */ public static QName[] getMemberTypeQNames(Node node) { String attribute = getAttribute(node, "memberTypes"); if (attribute == null) { return null; } StringTokenizer tokenizer = new StringTokenizer(attribute, " "); QName[] memberTypes = new QName[tokenizer.countTokens()];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -