Author: vitold
Date: 2009-03-24 06:09:28 -0400 (Tue, 24 Mar 2009)
New Revision: 2177
Modified:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java
Log:
add methods and stubs for Produces tag
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java 2009-03-24
08:38:34 UTC (rev 2176)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java 2009-03-24
10:09:28 UTC (rev 2177)
@@ -15,28 +15,30 @@
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.Namespace;
+import org.dom4j.QName;
import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.resources.spi.ResourceLoadingException;
public class ParseXmlHelper
-{
+{
public static boolean isJavaEeNamespace(Element element)
{
return
element.getNamespace().getURI().equalsIgnoreCase(XmlConstants.JAVA_EE_NAMESPACE);
}
-
+
public static <T> AnnotatedClass<? extends T> loadElementClass(Element
element, Class<T> expectedType, XmlEnvironment environment, Map<String,
Set<String>> packagesMap)
{
List<AnnotatedClass<? extends T>> classesList = new
ArrayList<AnnotatedClass<? extends T>>();
String className = element.getName();
String prefix = element.getNamespacePrefix();
-
- for(Map.Entry<String, Set<String>> packagesEntry :
packagesMap.entrySet())
+
+ for (Map.Entry<String, Set<String>> packagesEntry :
packagesMap.entrySet())
{
- if(prefix.equalsIgnoreCase(packagesEntry.getKey()))
+ if (prefix.equalsIgnoreCase(packagesEntry.getKey()))
{
Set<String> packages = packagesEntry.getValue();
- for(String packageName : packages)
+ for (String packageName : packages)
{
String classPath = packageName + "." + element.getName();
try
@@ -44,68 +46,73 @@
AnnotatedClass<? extends T> classType =
environment.loadClass(classPath, expectedType);
classesList.add(classType);
}
- catch(ResourceLoadingException e){}
+ catch (ResourceLoadingException e)
+ {
+ }
}
}
}
-
- if(classesList.size() == 0)
+
+ if (classesList.size() == 0)
throw new DefinitionException("Could not find '" + className +
"'");
-
- if(classesList.size() == 1)
+
+ if (classesList.size() == 1)
return classesList.get(0);
-
+
throw new DefinitionException("There are multiple packages containing a Java
type with the same name '" + className + "'");
}
-
+
+ public static void checkProduces(Element element, AnnotatedClass<?> beanClass)
+ {
+ }
+
public static void checkRootAttributes(Element root, Map<String,
Set<String>> packagesMap)
{
Iterator<?> rootAttrIterator = root.attributeIterator();
- while(rootAttrIterator.hasNext())
+ while (rootAttrIterator.hasNext())
{
Set<String> packagesSet = new HashSet<String>();
- Attribute attribute = (Attribute)rootAttrIterator.next();
- String attrPrefix = attribute.getNamespacePrefix();
+ Attribute attribute = (Attribute) rootAttrIterator.next();
+ String attrPrefix = attribute.getNamespacePrefix();
String attrData = attribute.getStringValue();
-
+
String urn = "";
- for(String attrVal : attrData.split(" "))
+ for (String attrVal : attrData.split(" "))
{
- if(attrVal.startsWith(XmlConstants.URN_PREFIX))
+ if (attrVal.startsWith(XmlConstants.URN_PREFIX))
{
urn = attrVal;
URL namespaceFile = loadFile(urn, XmlConstants.NAMESPACE_FILE_NAME);
- if(namespaceFile == null)
+ if (namespaceFile == null)
throw new DefinitionException("Could not find '" +
XmlConstants.NAMESPACE_FILE_NAME + "' file according to specified URN '"
+ urn + "'");
packagesSet.addAll(parseNamespaceFile(namespaceFile));
}
- if(attribute.getName().equalsIgnoreCase(XmlConstants.SCHEMA_LOCATION)
&&
- attrVal.startsWith(XmlConstants.HTTP_PREFIX) &&
urn.trim().length() > 0)
+ if (attribute.getName().equalsIgnoreCase(XmlConstants.SCHEMA_LOCATION)
&& attrVal.startsWith(XmlConstants.HTTP_PREFIX) && urn.trim().length()
> 0)
{
URL schemaFile = loadFile(urn, XmlConstants.SCHEMA_FILE_NAME);
- if(schemaFile == null)
+ if (schemaFile == null)
throw new DefinitionException("Could not find '" +
XmlConstants.SCHEMA_FILE_NAME + "' file according to specified URN '" +
urn + "'");
}
}
-
+
addElementToPackagesMap(packagesMap, attrPrefix, packagesSet);
}
}
-
+
public static void checkRootDeclaredNamespaces(Element root, Map<String,
Set<String>> packagesMap)
{
Iterator<?> namespacesIterator = root.declaredNamespaces().iterator();
- while(namespacesIterator.hasNext())
+ while (namespacesIterator.hasNext())
{
- Namespace namespace = (Namespace)namespacesIterator.next();
+ Namespace namespace = (Namespace) namespacesIterator.next();
String prefix = namespace.getPrefix();
String uri = namespace.getURI();
- if(uri.startsWith(XmlConstants.URN_PREFIX))
+ if (uri.startsWith(XmlConstants.URN_PREFIX))
{
Set<String> packagesSet = new HashSet<String>();
-
+
URL namespaceFile = loadFile(uri, XmlConstants.NAMESPACE_FILE_NAME);
- if(namespaceFile != null)
+ if (namespaceFile != null)
{
packagesSet.addAll(parseNamespaceFile(namespaceFile));
}
@@ -113,23 +120,62 @@
{
String packageName = uri.replaceFirst(XmlConstants.URN_PREFIX,
"");
packagesSet.add(packageName);
- }
-
+ }
+
addElementToPackagesMap(packagesMap, prefix, packagesSet);
}
}
}
+
+ public static List<Element> findElementsInEeNamespace(Element elementParent,
String elementName)
+ {
+ String elementPrefix = "";
+ String elementUri = XmlConstants.JAVA_EE_NAMESPACE;
+
+ return findElements(elementParent, elementName, elementPrefix, elementUri);
+ }
+ public static List<Element> findElements(Element elementParent, String
elementName, String elementPrefix, String elementUri)
+ {
+ List<Element> elements = new ArrayList<Element>();
+ Namespace elementNamespace = new Namespace(elementPrefix, elementUri);
+ QName qName = new QName(elementName, elementNamespace);
+ Iterator<?> elementIterator = elementParent.elementIterator(qName);
+ while(elementIterator.hasNext())
+ {
+ Element element = (Element)elementIterator.next();
+ elements.add(element);
+ }
+
+ return elements;
+ }
+
+ public static boolean isField(Element element, AnnotatedClass<?> beanClass,
AnnotatedClass<?> expectedType)
+ {
+ //TODO
+ String fieldName = element.getParent().getName();
+ AnnotatedField<?> beanField = beanClass.getDeclaredField(fieldName,
expectedType);
+ if(beanField != null)
+ return true;
+ return false;
+ }
+
+ public static boolean isMethod(Element element, AnnotatedClass<?> beanClass,
AnnotatedClass<?> expectedType)
+ {
+ //TODO
+ return false;
+ }
+
private static URL loadFile(String urn, String fileName)
{
char separator = '/';
String packageName = urn.replaceFirst(XmlConstants.URN_PREFIX, "");
String path = packageName.replace('.', separator);
String filePath = separator + path + separator + fileName;
- URL namespaceFile = ParseXmlHelper.class.getResource(filePath);
+ URL namespaceFile = ParseXmlHelper.class.getResource(filePath);
return namespaceFile;
}
-
+
private static Set<String> parseNamespaceFile(URL namespaceFile)
{
Set<String> packages = new HashSet<String>();
@@ -137,12 +183,12 @@
try
{
fileScanner = new Scanner(namespaceFile.openStream());
- while (fileScanner.hasNextLine() )
+ while (fileScanner.hasNextLine())
{
String line = fileScanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(XmlConstants.NAMESPACE_FILE_DELIMETER);
- while(lineScanner.hasNext())
+ while (lineScanner.hasNext())
{
packages.add(lineScanner.next());
}
@@ -154,12 +200,12 @@
catch (IOException e)
{
throw new RuntimeException("Error opening " +
namespaceFile.toString());
- }
+ }
}
-
+
private static void addElementToPackagesMap(Map<String, Set<String>>
packagesMap, String prefix, Set<String> packagesSet)
{
- if(packagesMap.containsKey(prefix))
+ if (packagesMap.containsKey(prefix))
{
Set<String> packages = packagesMap.get(prefix);
packages.addAll(packagesSet);
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java 2009-03-24
08:38:34 UTC (rev 2176)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlConstants.java 2009-03-24
10:09:28 UTC (rev 2177)
@@ -49,4 +49,6 @@
public static final String SCHEMA_FILE_NAME = "schema.xsd";
public static final String NAMESPACE_FILE_DELIMETER = " ";
+
+ public static final String PRODUCES = "Produces";
}
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java 2009-03-24 08:38:34
UTC (rev 2176)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java 2009-03-24 10:09:28
UTC (rev 2177)
@@ -14,12 +14,11 @@
import javax.inject.DefinitionException;
import javax.inject.DeploymentException;
+import javax.inject.manager.Bean;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
-import org.dom4j.Namespace;
-import org.dom4j.QName;
import org.dom4j.io.SAXReader;
import org.jboss.webbeans.introspector.AnnotatedClass;
import org.jboss.webbeans.log.Log;
@@ -61,6 +60,7 @@
for (Element beanElement : beanElements)
{
AnnotatedClass<?> beanClass = ParseXmlHelper.loadElementClass(beanElement,
Object.class, environment, packagesMap);
+ checkProduces(beanElement, beanClass);
beanClasses.add(beanClass);
}
@@ -168,13 +168,8 @@
if(deployElements.size() - deployElementsSet.size() != 0)
throw new DefinitionException("The same deployment type is declared more
than once");
- String standardName = XmlConstants.STANDARD;
- String standardPrefix = "";
- String standardUri = XmlConstants.JAVA_EE_NAMESPACE;
- Namespace standardNamespace = new Namespace(standardPrefix, standardUri);
- QName qName = new QName(standardName, standardNamespace);
- Element standardElement = element.element(qName);
- if (standardElement == null)
+ List<Element> standardElements =
ParseXmlHelper.findElementsInEeNamespace(element, XmlConstants.STANDARD);
+ if (standardElements.size() == 0)
throw new DeploymentException("The @Standard deployment type must be
declared");
List<Class<? extends Annotation>> deploymentClasses = new
ArrayList<Class<? extends Annotation>>();
@@ -192,6 +187,35 @@
return deploymentClasses;
}
+ public void checkProduces(Element beanElement, AnnotatedClass<?> beanClass)
+ {
+ Iterator<?> childIterator = beanElement.elementIterator();
+ while(childIterator.hasNext())
+ {
+ Element beanChild = (Element)childIterator.next();
+ List<Element> producesElements =
ParseXmlHelper.findElementsInEeNamespace(beanChild, XmlConstants.PRODUCES);
+
+ if(producesElements.size() == 0)
+ continue;
+
+ if(producesElements.size() > 1)
+ throw new DefinitionException("There is more than one child
<Produces> element for <" + beanChild.getName() + ">
element");
+
+ Element producesElement = producesElements.get(0);
+
+ if(ParseXmlHelper.isField(producesElement, beanClass, beanClass))
+ {
+ if(beanChild.elements().size() > 1)
+ throw new DefinitionException("There is more than one direct child
element for producer field <" + beanChild.getName() + ">");
+ }
+
+ if(ParseXmlHelper.isMethod(producesElement, beanClass, beanClass))
+ {}
+
+ throw new DefinitionException("A producer doesn't declared in class
file as method or field");
+ }
+ }
+
private void fullFillPackagesMap(Document document)
{
Element root = document.getRootElement();