Author: nickarls
Date: 2009-03-17 04:51:29 -0400 (Tue, 17 Mar 2009)
New Revision: 2062
Added:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java
Removed:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/NamespaceGenerator.java
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
Log:
minor. renames/moves
Copied: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java
(from rev 2061,
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/NamespaceGenerator.java)
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java
(rev 0)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java 2009-03-17
08:51:29 UTC (rev 2062)
@@ -0,0 +1,170 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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.jboss.webbeans.xsd;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Helper for generating and keeping track of namespaces in a schema
+ *
+ * @author Nicklas Karlsson
+ *
+ */
+public class NamespaceHandler
+{
+ // The set of reserved EE packages
+ private static final Set<String> URN_JAVA_EE = new
HashSet<String>(Arrays.asList("java.lang", "java.util",
"javax.annotation", "javax.inject", "javax.context",
"javax.interceptor", "javax.decorator", "javax.event",
"javax.ejb", "javax.persistence", "javax.xml.ws",
"javax.jms", "javax.sql"));
+
+ // The local package of the scema
+ private String localPackage;
+
+ // Duplicate shortname counters
+ private Map<String, Integer> counters = new HashMap<String, Integer>();
+ // Namespace infos
+ private Map<String, SchemaNamespace> schemaNamespaces = new HashMap<String,
SchemaNamespace>();
+
+ /**
+ * Creats a new namespace generator
+ *
+ * @param localPackage The local package
+ */
+ public NamespaceHandler(String localPackage)
+ {
+ this.localPackage = localPackage;
+ }
+
+ /**
+ * Data for a package namespace
+ *
+ * @author Nicklas Karlsson
+ *
+ */
+ private class SchemaNamespace
+ {
+ // The package name
+ String packageName;
+ // The full namespace
+ String namespace;
+ // The namespace abbreviation
+ String shortNamespace;
+ // Is this a EE reserved package?
+ boolean ee;
+
+ public SchemaNamespace(String packageName, String shortNamespace, boolean ee)
+ {
+ this.packageName = packageName;
+ this.shortNamespace = shortNamespace;
+ // Skip ":" for default namespace
+ String colon = "".equals(shortNamespace) ? "" :
":";
+ // Hardcode "ee" for EE reserved packages
+ String url = ee ? "ee" : packageName;
+ this.namespace = "xmlns" + colon + shortNamespace +
"=\"urn:java:" + url + "\"";
+ this.ee = ee;
+ }
+ }
+
+ /**
+ * Gets all used namespaces for the schema
+ *
+ * @return The used namespaces
+ */
+ public Set<String> getUsedNamespaces()
+ {
+ Set<String> usedNamespaces = new HashSet<String>();
+ for (SchemaNamespace schemaNamespace : schemaNamespaces.values())
+ {
+ usedNamespaces.add(schemaNamespace.namespace);
+ }
+ return usedNamespaces;
+ }
+
+ /**
+ * Gets a namespace abbreviation for a package
+ *
+ * @param packageName The name of the package
+ * @return The namespace abbreviation
+ */
+ public String getShortNamespace(String packageName)
+ {
+ if (schemaNamespaces.containsKey(packageName))
+ {
+ return schemaNamespaces.get(packageName).shortNamespace;
+ }
+ String shortNamespace = "";
+ boolean ee = false;
+ if (localPackage.equals(packageName))
+ {
+ // Nothing to do but want to hit this case first for performance
+ }
+ else if (URN_JAVA_EE.contains(packageName))
+ {
+ shortNamespace = "ee";
+ ee = true;
+ }
+ else
+ {
+ String shortName = getShortName(packageName);
+ Integer count = counters.get(shortName);
+ String countString = "";
+ if (count == null)
+ {
+ count = new Integer(1);
+ counters.put(shortName, count);
+ }
+ else
+ {
+ count++;
+ countString = String.valueOf(count);
+ }
+ shortNamespace = getShortName(packageName) + countString;
+ }
+ schemaNamespaces.put(packageName, new SchemaNamespace(packageName, shortNamespace,
ee));
+ return shortNamespace;
+ }
+
+ /**
+ * Gets the short name (last part) of a package
+ *
+ * @param packageName The package name to parse
+ * @return The short name
+ */
+ private String getShortName(String packageName)
+ {
+ int lastDot = packageName.lastIndexOf(".");
+ return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
+ }
+
+ // TODO testing, remove
+ public static void main(String[] params)
+ {
+ NamespaceHandler ng = new NamespaceHandler("com.acme.foo");
+ System.out.println(ng.getShortNamespace("com.acme.foo"));
+ System.out.println(ng.getShortNamespace("com.acme.foo.foo"));
+ System.out.println(ng.getShortNamespace("com.acme.foo.foo.foo"));
+ System.out.println(ng.getShortNamespace("java.util"));
+ for (String ns : ng.getUsedNamespaces())
+ {
+ System.out.println(ns);
+ }
+ }
+
+}
Property changes on:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/NamespaceHandler.java
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java 2009-03-17
07:38:35 UTC (rev 2061)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java 2009-03-17
08:51:29 UTC (rev 2062)
@@ -1,105 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2008, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * 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.jboss.webbeans.xsd;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import org.dom4j.Document;
-import org.jboss.webbeans.xsd.helpers.NamespaceGenerator;
-import org.jboss.webbeans.xsd.model.TypedModel;
-
-/**
- * Package information
- *
- * @author Nicklas Karlsson
- *
- */
-public class PackageInfo
-{
- private Document schema;
- private String packageName;
- private Map<String, Set<String>> typeReferences;
- private NamespaceGenerator namespaceGenerator;
-
- public PackageInfo(String packageName)
- {
- this.packageName = packageName;
- typeReferences = new HashMap<String, Set<String>>();
- namespaceGenerator = new NamespaceGenerator(packageName);
- }
-
- public void addTypeReferences(Set<TypedModel> references)
- {
- for (TypedModel reference : references)
- {
- Set<String> typeNames = typeReferences.get(reference.getTypePackage());
- if (typeNames == null)
- {
- typeNames = new HashSet<String>();
- typeReferences.put(reference.getTypePackage(), typeNames);
- }
- typeNames.add(reference.getType());
- }
- }
-
- public Document getSchema()
- {
- return schema;
- }
-
- public void setSchema(Document schema)
- {
- this.schema = schema;
- }
-
- public String getPackageName()
- {
- return packageName;
- }
-
- public void setPackageName(String packageName)
- {
- this.packageName = packageName;
- }
-
- public Map<String, Set<String>> getTypeReferences()
- {
- return typeReferences;
- }
-
- // TODO: dummy, remove
- public void refreshNamespaces()
- {
- for (String p : typeReferences.keySet())
- {
- if (!"".equals(p))
- {
- String dummy = namespaceGenerator.getShortNamespace(p);
- }
- }
- }
-
- public Set<String> getNamespaces()
- {
- return namespaceGenerator.getUsedNamespaces();
- }
-
-}
Copied: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java (from rev
2061, extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageInfo.java)
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java
(rev 0)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java 2009-03-17
08:51:29 UTC (rev 2062)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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.jboss.webbeans.xsd;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.dom4j.Document;
+import org.jboss.webbeans.xsd.model.TypedModel;
+
+/**
+ * Package information
+ *
+ * @author Nicklas Karlsson
+ *
+ */
+public class Schema
+{
+ private Document document;
+ private String packageName;
+ private Map<String, Set<String>> typeReferences;
+ private NamespaceHandler namespaceHandler;
+
+ public Schema(String packageName)
+ {
+ this.packageName = packageName;
+ typeReferences = new HashMap<String, Set<String>>();
+ namespaceHandler = new NamespaceHandler(packageName);
+ }
+
+ public void addTypeReferences(Set<TypedModel> references)
+ {
+ for (TypedModel reference : references)
+ {
+ Set<String> typeNames = typeReferences.get(reference.getTypePackage());
+ if (typeNames == null)
+ {
+ typeNames = new HashSet<String>();
+ typeReferences.put(reference.getTypePackage(), typeNames);
+ }
+ typeNames.add(reference.getType());
+ }
+ }
+
+ public Document getDocument()
+ {
+ return document;
+ }
+
+ public void setDocument(Document document)
+ {
+ this.document = document;
+ }
+
+ public String getPackageName()
+ {
+ return packageName;
+ }
+
+ public void setPackageName(String packageName)
+ {
+ this.packageName = packageName;
+ }
+
+ public Map<String, Set<String>> getTypeReferences()
+ {
+ return typeReferences;
+ }
+
+ // TODO: dummy, remove
+ public void refreshNamespaces()
+ {
+ for (String p : typeReferences.keySet())
+ {
+ if (!"".equals(p))
+ {
+ String dummy = namespaceHandler.getShortNamespace(p);
+ }
+ }
+ }
+
+ public Set<String> getNamespaces()
+ {
+ return namespaceHandler.getUsedNamespaces();
+ }
+
+}
Property changes on:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/NamespaceGenerator.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/NamespaceGenerator.java 2009-03-17
07:38:35 UTC (rev 2061)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/NamespaceGenerator.java 2009-03-17
08:51:29 UTC (rev 2062)
@@ -1,170 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2008, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * 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.jboss.webbeans.xsd.helpers;
-
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Helper for generating and keeping track of namespaces in a schema
- *
- * @author Nicklas Karlsson
- *
- */
-public class NamespaceGenerator
-{
- // The set of reserved EE packages
- private static final Set<String> URN_JAVA_EE = new
HashSet<String>(Arrays.asList("java.lang", "java.util",
"javax.annotation", "javax.inject", "javax.context",
"javax.interceptor", "javax.decorator", "javax.event",
"javax.ejb", "javax.persistence", "javax.xml.ws",
"javax.jms", "javax.sql"));
-
- // The local package of the scema
- private String localPackage;
-
- // Duplicate shortname counters
- private Map<String, Integer> counters = new HashMap<String, Integer>();
- // Namespace infos
- private Map<String, NamespaceInfo> packageNamespaceInfo = new HashMap<String,
NamespaceInfo>();
-
- /**
- * Creats a new namespace generator
- *
- * @param localPackage The local package
- */
- public NamespaceGenerator(String localPackage)
- {
- this.localPackage = localPackage;
- }
-
- /**
- * Data for a package namespace
- *
- * @author Nicklas Karlsson
- *
- */
- private class NamespaceInfo
- {
- // The package name
- String packageName;
- // The full namespace
- String namespace;
- // The namespace abbreviation
- String shortNamespace;
- // Is this a EE reserved package?
- boolean ee;
-
- public NamespaceInfo(String packageName, String shortNamespace, boolean ee)
- {
- this.packageName = packageName;
- this.shortNamespace = shortNamespace;
- // Skip ":" for default namespace
- String colon = "".equals(shortNamespace) ? "" :
":";
- // Hardcode "ee" for EE reserved packages
- String url = ee ? "ee" : packageName;
- this.namespace = "xmlns" + colon + shortNamespace +
"=\"urn:java:" + url + "\"";
- this.ee = ee;
- }
- }
-
- /**
- * Gets all used namespaces for the schema
- *
- * @return The used namespaces
- */
- public Set<String> getUsedNamespaces()
- {
- Set<String> usedNamespaces = new HashSet<String>();
- for (NamespaceInfo namespaceInfo : packageNamespaceInfo.values())
- {
- usedNamespaces.add(namespaceInfo.namespace);
- }
- return usedNamespaces;
- }
-
- /**
- * Gets a namespace abbreviation for a package
- *
- * @param packageName The name of the package
- * @return The namespace abbreviation
- */
- public String getShortNamespace(String packageName)
- {
- if (packageNamespaceInfo.containsKey(packageName))
- {
- return packageNamespaceInfo.get(packageName).shortNamespace;
- }
- String shortNamespace = "";
- boolean ee = false;
- if (localPackage.equals(packageName))
- {
- // Nothing to do but want to hit this case first for performance
- }
- else if (URN_JAVA_EE.contains(packageName))
- {
- shortNamespace = "ee";
- ee = true;
- }
- else
- {
- String shortName = getShortName(packageName);
- Integer count = counters.get(shortName);
- String countString = "";
- if (count == null)
- {
- count = new Integer(1);
- counters.put(shortName, count);
- }
- else
- {
- count++;
- countString = String.valueOf(count);
- }
- shortNamespace = getShortName(packageName) + countString;
- }
- packageNamespaceInfo.put(packageName, new NamespaceInfo(packageName,
shortNamespace, ee));
- return shortNamespace;
- }
-
- /**
- * Gets the short name (last part) of a package
- *
- * @param packageName The package name to parse
- * @return The short name
- */
- private String getShortName(String packageName)
- {
- int lastDot = packageName.lastIndexOf(".");
- return lastDot < 0 ? packageName : packageName.substring(lastDot + 1);
- }
-
- // TODO testing, remove
- public static void main(String[] params)
- {
- NamespaceGenerator ng = new NamespaceGenerator("com.acme.foo");
- System.out.println(ng.getShortNamespace("com.acme.foo"));
- System.out.println(ng.getShortNamespace("com.acme.foo.foo"));
- System.out.println(ng.getShortNamespace("com.acme.foo.foo.foo"));
- System.out.println(ng.getShortNamespace("java.util"));
- for (String ns : ng.getUsedNamespaces())
- {
- System.out.println(ns);
- }
- }
-
-}
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java 2009-03-17
07:38:35 UTC (rev 2061)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java 2009-03-17
08:51:29 UTC (rev 2062)
@@ -34,7 +34,7 @@
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
-import org.jboss.webbeans.xsd.PackageInfo;
+import org.jboss.webbeans.xsd.Schema;
import org.jboss.webbeans.xsd.model.ClassModel;
/**
@@ -51,7 +51,7 @@
// The cache of already processed classes
private Map<String, ClassModel> classModelCache = new HashMap<String,
ClassModel>();
// The XSD documents of the affected packages
- private Map<String, PackageInfo> packageInfoMap = new HashMap<String,
PackageInfo>();
+ private Map<String, Schema> schemaMap = new HashMap<String, Schema>();
/**
* Creates a new helper
@@ -71,12 +71,12 @@
* @throws DocumentException If the schema could not be parsed
* @throws IOException If the schema could not be read
*/
- private PackageInfo readPackageInfo(String packageName) throws DocumentException,
IOException
+ private Schema getSchema(String packageName) throws DocumentException, IOException
{
- PackageInfo packageInfo = new PackageInfo(packageName);
- Document schema = readSchema(packageName);
- packageInfo.setSchema(schema != null ? schema : createSchema(packageName));
- return packageInfo;
+ Schema schema = new Schema(packageName);
+ Document document = readSchema(packageName);
+ schema.setDocument(document != null ? document : createSchema(packageName));
+ return schema;
}
/**
@@ -124,17 +124,17 @@
/**
* Writes package info to the disk
*
- * @param packageInfo The package info to store
+ * @param schema The package info to store
*/
- private void writePackageInfo(PackageInfo packageInfo)
+ private void writePackageInfo(Schema schema)
{
try
{
- writeSchema(packageInfo.getPackageName(), packageInfo.getSchema());
+ writeSchema(schema.getPackageName(), schema.getDocument());
}
catch (IOException e)
{
- throw new RuntimeException("Could not write schema for " +
packageInfo.getPackageName());
+ throw new RuntimeException("Could not write schema for " +
schema.getPackageName());
}
}
@@ -176,12 +176,12 @@
for (ClassModel classModel : classModels)
{
String packageName = classModel.getPackage();
- PackageInfo packageInfo = packageInfoMap.get(packageName);
- if (packageInfo == null)
+ Schema schema = schemaMap.get(packageName);
+ if (schema == null)
{
try
{
- packageInfo = readPackageInfo(packageName);
+ schema = getSchema(packageName);
}
catch (DocumentException e)
{
@@ -191,9 +191,9 @@
{
throw new RuntimeException("Could not read schema for package "
+ packageName);
}
- packageInfoMap.put(packageName, packageInfo);
+ schemaMap.put(packageName, schema);
}
- updateClassInSchema(classModel, packageInfo);
+ updateClassInSchema(classModel, schema);
}
}
@@ -202,34 +202,34 @@
*/
public void writeSchemas()
{
- for (PackageInfo packageInfo : packageInfoMap.values())
+ for (Schema schema : schemaMap.values())
{
// TODO: dummy, remove
- packageInfo.refreshNamespaces();
- System.out.println(packageInfo.getPackageName() + " (" +
packageInfo.getNamespaces() + ")");
- System.out.println(packageInfo.getTypeReferences());
- writePackageInfo(packageInfo);
+ schema.refreshNamespaces();
+ System.out.println(schema.getPackageName() + " (" +
schema.getNamespaces() + ")");
+ System.out.println(schema.getTypeReferences());
+ writePackageInfo(schema);
}
}
/**
* Updates a schema with XSD from a file model
*
- * @param packageInfo The schema
+ * @param schema The schema
* @param classModel The class model
*/
- private void updateClassInSchema(ClassModel classModel, PackageInfo packageInfo)
+ private void updateClassInSchema(ClassModel classModel, Schema schema)
{
- Document schema = packageInfo.getSchema();
- Node oldClassModel = schema.selectSingleNode("//" +
classModel.getSimpleName());
+ Document document = schema.getDocument();
+ Node oldClassModel = document.selectSingleNode("//" +
classModel.getSimpleName());
if (oldClassModel != null)
{
// Remove the old class definition
- schema.getRootElement().remove(oldClassModel);
+ document.getRootElement().remove(oldClassModel);
}
// Create a new one
- schema.getRootElement().addElement(classModel.getSimpleName());
- packageInfo.addTypeReferences(classModel.getTypeReferences());
+ document.getRootElement().addElement(classModel.getSimpleName());
+ schema.addTypeReferences(classModel.getTypeReferences());
}
/**