1 17 package org.apache.ws.jaxme.generator.sg.impl; 18 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 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 ; 34 import org.xml.sax.SAXException ; 35 36 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 getClassNameFromLocalName(Locator pLocator, String 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 getPackageName(SchemaSG pSchemaSG, 70 JAXBSchemaBindings pSchemaBindings, 71 Locator pLocator, 72 XsQName pQName) 73 throws SAXException { 74 String 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 getPackageNameFromURL(URL pURL) { 91 List tokens = new ArrayList (); 92 for (StringTokenizer st = new StringTokenizer (pURL.getHost(), "."); 93 st.hasMoreTokens(); ) { 94 tokens.add(st.nextToken()); 95 } 96 Collections.reverse(tokens); 97 for (StringTokenizer st = new StringTokenizer (pURL.getPath(), "/"); 98 st.hasMoreTokens(); ) { 99 tokens.add(st.nextToken()); 100 } 101 102 if (tokens.size() == 0) { 103 throw new IllegalArgumentException ("Could not parse URL " + pURL); 104 } 105 StringBuffer sb = new StringBuffer (); 106 for (int i = 0; i < tokens.size(); i++) { 107 if (i > 0) { 108 sb.append('.'); 109 } 110 String tok = (String ) tokens.get(i); 111 if (tok.length() == 0) { 112 throw new IllegalArgumentException ("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 getPackageNameFromURI(Locator pLocator, String 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 url = new java.net.URL (pURI); 133 return getPackageNameFromURL(url); 134 } catch (MalformedURLException 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 toString() { 141 return super.toString() + " [" + getName() + "," + getXMLInterfaceName() + "," + 142 getXMLImplementationName() + "]"; 143 } 144 } 145 | Popular Tags |