001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.wsdl.parser;
038:
039: import com.sun.tools.ws.wsdl.document.WSDLConstants;
040: import com.sun.tools.ws.wsdl.document.jaxws.JAXWSBindingsConstants;
041: import com.sun.tools.ws.wsdl.document.schema.SchemaConstants;
042: import com.sun.tools.xjc.util.DOMUtils;
043: import org.w3c.dom.Element;
044: import org.w3c.dom.NodeList;
045: import org.xml.sax.Attributes;
046: import org.xml.sax.helpers.XMLFilterImpl;
047:
048: /**
049: * @author Vivek Pandey
050: */
051: public class WSDLInternalizationLogic implements InternalizationLogic {
052:
053: /**
054: * This filter looks for <xs:import> and <xs:include>
055: * and parses those documents referenced by them.
056: */
057: private static final class ReferenceFinder extends
058: AbstractReferenceFinderImpl {
059: ReferenceFinder(DOMForest parent) {
060: super (parent);
061: }
062:
063: protected String findExternalResource(String nsURI,
064: String localName, Attributes atts) {
065: if (WSDLConstants.NS_WSDL.equals(nsURI)
066: && "import".equals(localName)) {
067: if (parent.isExtensionMode()) {
068: //TODO: add support for importing schema using wsdl:import
069: }
070: return atts.getValue("location");
071: } else if (SchemaConstants.NS_XSD.equals(nsURI)
072: && "import".equals(localName)) {
073: return atts.getValue("schemaLocation");
074: }
075: return null;
076: }
077: }
078:
079: public XMLFilterImpl createExternalReferenceFinder(DOMForest parent) {
080: return new ReferenceFinder(parent);
081: }
082:
083: public boolean checkIfValidTargetNode(DOMForest parent,
084: Element bindings, Element target) {
085: return false;
086: }
087:
088: public Element refineSchemaTarget(Element target) {
089: // look for existing xs:annotation
090: Element annotation = DOMUtils.getFirstChildElement(target,
091: Constants.NS_XSD, "annotation");
092: if (annotation == null)
093: // none exists. need to make one
094: annotation = insertXMLSchemaElement(target, "annotation");
095:
096: // then look for appinfo
097: Element appinfo = DOMUtils.getFirstChildElement(annotation,
098: Constants.NS_XSD, "appinfo");
099: if (appinfo == null)
100: // none exists. need to make one
101: appinfo = insertXMLSchemaElement(annotation, "appinfo");
102:
103: return appinfo;
104:
105: }
106:
107: public Element refineWSDLTarget(Element target) {
108: // look for existing xs:annotation
109: Element JAXWSBindings = DOMUtils.getFirstChildElement(target,
110: JAXWSBindingsConstants.NS_JAXWS_BINDINGS, "bindings");
111: if (JAXWSBindings == null)
112: // none exists. need to make one
113: JAXWSBindings = insertJAXWSBindingsElement(target,
114: "bindings");
115: return JAXWSBindings;
116: }
117:
118: private Element insertJAXWSBindingsElement(Element parent,
119: String localName) {
120: String qname = "JAXWS:" + localName;
121:
122: Element child = parent.getOwnerDocument().createElementNS(
123: JAXWSBindingsConstants.NS_JAXWS_BINDINGS, qname);
124:
125: NodeList children = parent.getChildNodes();
126:
127: if (children.getLength() == 0)
128: parent.appendChild(child);
129: else
130: parent.insertBefore(child, children.item(0));
131:
132: return child;
133: }
134:
135: /**
136: * Creates a new XML Schema element of the given local name
137: * and insert it as the first child of the given parent node.
138: *
139: * @return
140: * Newly create element.
141: */
142: private Element insertXMLSchemaElement(Element parent,
143: String localName) {
144: // use the same prefix as the parent node to avoid modifying
145: // the namespace binding.
146: String qname = parent.getTagName();
147: int idx = qname.indexOf(':');
148: if (idx == -1)
149: qname = localName;
150: else
151: qname = qname.substring(0, idx + 1) + localName;
152:
153: Element child = parent.getOwnerDocument().createElementNS(
154: Constants.NS_XSD, qname);
155:
156: NodeList children = parent.getChildNodes();
157:
158: if (children.getLength() == 0)
159: parent.appendChild(child);
160: else
161: parent.insertBefore(child, children.item(0));
162:
163: return child;
164: }
165:
166: }
|