001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.description;
020:
021: import org.apache.axis2.AxisFault;
022: import org.apache.axis2.namespace.Constants;
023: import org.apache.neethi.PolicyRegistry;
024: import org.apache.ws.commons.schema.XmlSchema;
025: import org.apache.ws.commons.schema.XmlSchemaCollection;
026: import org.apache.ws.commons.schema.resolver.URIResolver;
027: import org.w3c.dom.Element;
028:
029: import javax.xml.namespace.QName;
030: import javax.xml.parsers.DocumentBuilder;
031: import javax.xml.parsers.DocumentBuilderFactory;
032: import javax.xml.parsers.ParserConfigurationException;
033: import java.io.InputStream;
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.Map;
037:
038: public abstract class WSDLToAxisServiceBuilder {
039:
040: protected static final String XMLSCHEMA_NAMESPACE_URI = Constants.URI_2001_SCHEMA_XSD;
041:
042: protected static final String XMLSCHEMA_NAMESPACE_PREFIX = "xs";
043:
044: protected static final String XML_SCHEMA_LOCAL_NAME = "schema";
045:
046: protected static final String XML_SCHEMA_SEQUENCE_LOCAL_NAME = "sequence";
047:
048: protected static final String XML_SCHEMA_COMPLEX_TYPE_LOCAL_NAME = "complexType";
049:
050: protected static final String XML_SCHEMA_ELEMENT_LOCAL_NAME = "element";
051:
052: protected static final String XML_SCHEMA_IMPORT_LOCAL_NAME = "import";
053:
054: protected static final String XSD_NAME = "name";
055:
056: protected static final String XSD_TARGETNAMESPACE = "targetNamespace";
057:
058: protected static final String XMLNS_AXIS2WRAPPED = "xmlns:axis2wrapped";
059:
060: protected static final String AXIS2WRAPPED = "axis2wrapped";
061:
062: protected static final String XSD_TYPE = "type";
063:
064: protected static final String XSD_REF = "ref";
065:
066: protected static int nsCount = 0;
067:
068: protected Map resolvedRpcWrappedElementMap = new HashMap();
069:
070: protected static final String XSD_ELEMENT_FORM_DEFAULT = "elementFormDefault";
071:
072: protected static final String XSD_UNQUALIFIED = "unqualified";
073:
074: protected InputStream in;
075:
076: protected AxisService axisService;
077:
078: protected PolicyRegistry registry;
079:
080: protected QName serviceName;
081: protected boolean isServerSide = true;
082: protected String style = null;
083: private URIResolver customResolver;
084: private String baseUri = null;
085: protected static final String TYPES = "Types";
086:
087: // keeping whether builder is used in codegen or not
088: protected boolean isCodegen;
089:
090: protected WSDLToAxisServiceBuilder() {
091:
092: }
093:
094: public WSDLToAxisServiceBuilder(InputStream in, QName serviceName) {
095: this .in = in;
096: this .serviceName = serviceName;
097: this .axisService = new AxisService();
098: setPolicyRegistryFromService(axisService);
099: }
100:
101: public WSDLToAxisServiceBuilder(InputStream in,
102: AxisService axisService) {
103: this .in = in;
104: this .axisService = axisService;
105: setPolicyRegistryFromService(axisService);
106: }
107:
108: /**
109: * Sets a custom xmlschema URI resolver
110: *
111: * @param customResolver a URIResolver to use when working with schemas
112: */
113: public void setCustomResolver(URIResolver customResolver) {
114: this .customResolver = customResolver;
115: }
116:
117: public boolean isServerSide() {
118: return isServerSide;
119: }
120:
121: public void setServerSide(boolean serverSide) {
122: isServerSide = serverSide;
123: }
124:
125: protected void setPolicyRegistryFromService(AxisService axisService) {
126: PolicyInclude policyInclude = axisService.getPolicyInclude();
127: this .registry = policyInclude.getPolicyRegistry();
128: }
129:
130: protected XmlSchema getXMLSchema(Element element, String baseUri) {
131: XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
132:
133: if (baseUri != null) {
134: schemaCollection.setBaseUri(baseUri);
135: }
136:
137: if (customResolver != null) {
138: schemaCollection.setSchemaResolver(customResolver);
139: }
140:
141: return schemaCollection.read(element);
142: }
143:
144: /**
145: * Find the XML schema prefix
146: *
147: * @return the active schema prefix, or the default schema prefix if not declared
148: */
149: protected String findSchemaPrefix() {
150: String xsdPrefix = null;
151: Map declaredNameSpaces = axisService.getNamespaceMap();
152: if (declaredNameSpaces.containsValue(XMLSCHEMA_NAMESPACE_URI)) {
153: //loop and find the prefix
154: Iterator it = declaredNameSpaces.keySet().iterator();
155: String key;
156: while (it.hasNext()) {
157: key = (String) it.next();
158: if (XMLSCHEMA_NAMESPACE_URI.equals(declaredNameSpaces
159: .get(key))) {
160: xsdPrefix = key;
161: break;
162: }
163: }
164: } else {
165: xsdPrefix = XMLSCHEMA_NAMESPACE_PREFIX; //default prefix
166: }
167: return xsdPrefix;
168: }
169:
170: public abstract AxisService populateService() throws AxisFault;
171:
172: /**
173: * Utility method that returns a DOM Builder
174: *
175: * @return a namespace-aware DOM DocumentBuilder
176: */
177: protected DocumentBuilder getDOMDocumentBuilder() {
178: DocumentBuilder documentBuilder;
179: try {
180: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
181: .newInstance();
182: documentBuilderFactory.setNamespaceAware(true);
183: documentBuilder = documentBuilderFactory
184: .newDocumentBuilder();
185: } catch (ParserConfigurationException e) {
186: throw new RuntimeException(e);
187: }
188: return documentBuilder;
189: }
190:
191: /**
192: * Get a temporary namespace prefix. NOT threadsafe.
193: *
194: * @return a new namespace prefix of the form "nsX"
195: */
196: protected String getTemporaryNamespacePrefix() {
197: return "ns" + nsCount++;
198: }
199:
200: public String getBaseUri() {
201: return baseUri;
202: }
203:
204: public void setBaseUri(String baseUri) {
205: this .baseUri = baseUri;
206: }
207:
208: public boolean isCodegen() {
209: return isCodegen;
210: }
211:
212: public void setCodegen(boolean codegen) {
213: isCodegen = codegen;
214: }
215:
216: public QName getServiceName() {
217: return serviceName;
218: }
219:
220: public void setServiceName(QName serviceName) {
221: this.serviceName = serviceName;
222: }
223: }
|