KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > sg > impl > AbstractContext


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.generator.sg.impl;
18
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.apache.ws.jaxme.generator.sg.Context;
27 import org.apache.ws.jaxme.generator.sg.SchemaSG;
28 import org.apache.ws.jaxme.generator.util.JavaNamer;
29 import org.apache.ws.jaxme.js.JavaQName;
30 import org.apache.ws.jaxme.xs.jaxb.JAXBSchemaBindings;
31 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
32 import org.apache.ws.jaxme.xs.xml.XsQName;
33 import org.xml.sax.Locator JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36 /**
37  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
38  */

39 public abstract class AbstractContext implements Context {
40     private XsQName name;
41     private JavaQName xmlInterfaceName, xmlImplementationName, xmlHandlerName, xmlSerializerName;
42     private JavaQName xmlValidatorName, pmName;
43
44     public XsQName getName() { return name; }
45     protected void setName(XsQName pName) { name = pName; }
46     public JavaQName getXMLHandlerName() { return xmlHandlerName; }
47     protected void setXMLHandlerName(JavaQName pXmlHandlerName) { xmlHandlerName = pXmlHandlerName; }
48     public JavaQName getXMLImplementationName() { return xmlImplementationName; }
49     protected void setXMLImplementationName(JavaQName pXmlImplementationName) { xmlImplementationName = pXmlImplementationName; }
50     public JavaQName getXMLInterfaceName() { return xmlInterfaceName; }
51     protected void setXMLInterfaceName(JavaQName pXmlInterfaceName) {
52         xmlInterfaceName = pXmlInterfaceName;
53     }
54     public JavaQName getXMLSerializerName() { return xmlSerializerName; }
55     protected void setXMLSerializerName(JavaQName pXmlSerializerName) { xmlSerializerName = pXmlSerializerName; }
56     protected void setXMLValidatorName(JavaQName pXmlValidatorName) { xmlValidatorName = pXmlValidatorName; }
57     public JavaQName getXMLValidatorName() { return xmlValidatorName; }
58     protected void setPMName(JavaQName pPMName) { pmName = pPMName; }
59     public JavaQName getPMName() { return pmName; }
60
61     protected static String JavaDoc getClassNameFromLocalName(Locator JavaDoc pLocator, String JavaDoc pLocalName,
62                                                       SchemaSG pSchemaSG) throws SAXException {
63         if (pLocalName == null || pLocalName.length() == 0) {
64             throw new LocSAXException("Invalid local name: " + pLocalName, pLocator);
65         }
66         return JavaNamer.convert(pLocalName, pSchemaSG);
67     }
68
69     protected static String JavaDoc getPackageName(SchemaSG pSchemaSG,
70                                            JAXBSchemaBindings pSchemaBindings,
71                                            Locator JavaDoc pLocator,
72                                            XsQName pQName)
73             throws SAXException {
74         String JavaDoc packageName = pSchemaSG.getFactory().getGenerator().getProperty("jaxme.package.name");
75         if (packageName == null) {
76             if (pSchemaBindings != null) {
77                 JAXBSchemaBindings.Package jaxbPackage = pSchemaBindings.getPackage();
78                 if (jaxbPackage != null) {
79                     packageName = jaxbPackage.getName();
80                 }
81             }
82         }
83       
84         if (packageName == null) {
85             packageName = getPackageNameFromURI(pLocator, pQName.getNamespaceURI());
86         }
87         return packageName;
88     }
89
90     protected static String JavaDoc getPackageNameFromURL(URL JavaDoc pURL) {
91         List JavaDoc tokens = new ArrayList JavaDoc();
92         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pURL.getHost(), ".");
93              st.hasMoreTokens(); ) {
94             tokens.add(st.nextToken());
95         }
96         Collections.reverse(tokens);
97         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pURL.getPath(), "/");
98              st.hasMoreTokens(); ) {
99             tokens.add(st.nextToken());
100         }
101   
102         if (tokens.size() == 0) {
103             throw new IllegalArgumentException JavaDoc("Could not parse URL " + pURL);
104         }
105         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
106         for (int i = 0; i < tokens.size(); i++) {
107             if (i > 0) {
108                 sb.append('.');
109             }
110             String JavaDoc tok = (String JavaDoc) tokens.get(i);
111             if (tok.length() == 0) {
112                 throw new IllegalArgumentException JavaDoc("Could not parse URL " + pURL);
113             }
114             for (int j = 0; j < tok.length(); j++) {
115                 char c = Character.toLowerCase(tok.charAt(j));
116                 if ((j == 0 && Character.isJavaIdentifierStart(c))
117                     || (j > 0 && Character.isJavaIdentifierPart(c))) {
118                     sb.append(c);
119                 } else {
120                     sb.append('_');
121                 }
122             }
123         }
124         return sb.toString();
125     }
126   
127     protected static String JavaDoc getPackageNameFromURI(Locator JavaDoc pLocator, String JavaDoc pURI) throws SAXException {
128         if (pURI == null || pURI.length() == 0) {
129             throw new LocSAXException("Unable to derive package name from an empty namespace URI. Use the schemaBindings to specify a package name.", pLocator);
130         }
131         try {
132             URL JavaDoc url = new java.net.URL JavaDoc(pURI);
133             return getPackageNameFromURL(url);
134         } catch (MalformedURLException JavaDoc e) {
135             throw new LocSAXException("Unable to derive package name from an URI, which is no URL: " + pURI,
136                     pLocator);
137         }
138     }
139     
140     public String JavaDoc toString() {
141         return super.toString() + " [" + getName() + "," + getXMLInterfaceName() + "," +
142         getXMLImplementationName() + "]";
143     }
144 }
145
Popular Tags