You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-cvs@xml.apache.org by pc...@apache.org on 2004/06/24 07:30:01 UTC
cvs commit: xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/internal/annotations ClassBindingInfo.java ElementTarget.java NamedTarget.java TypeTarget.java
pcal 2004/06/23 22:30:01
Modified: v2/jam/src/org/apache/xmlbeans/impl/jam/internal/elements
AnnotatedElementImpl.java
v2/jam/src/org/apache/xmlbeans/impl/jam/internal/javadoc
JavadocClassBuilder.java
v2/jam/src/org/apache/xmlbeans/impl/jam/internal/reflect
ReflectClassBuilder.java
v2/jam/src/org/apache/xmlbeans/impl/jam/mutable
MAnnotatedElement.java
v2/src/binding/org/apache/xmlbeans/impl/binding/compile
Java2Schema.java
Added: v2/src/binding/org/apache/xmlbeans/impl/binding/compile/internal
Java2SchemaAnnotationHelper.java
v2/src/binding/org/apache/xmlbeans/impl/binding/compile/internal/annotations
ClassBindingInfo.java ElementTarget.java
NamedTarget.java TypeTarget.java
Log:
j2s: 175 annotation types jam: tweaks for annotations
Revision Changes Path
1.21 +14 -40 xml-xmlbeans/v2/jam/src/org/apache/xmlbeans/impl/jam/internal/elements/AnnotatedElementImpl.java
Index: AnnotatedElementImpl.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/jam/src/org/apache/xmlbeans/impl/jam/internal/elements/AnnotatedElementImpl.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- AnnotatedElementImpl.java 8 Jun 2004 21:29:51 -0000 1.20
+++ AnnotatedElementImpl.java 24 Jun 2004 05:30:00 -0000 1.21
@@ -190,47 +190,21 @@
// Old stuff
/**
- * @deprecated
- *
- * @param tagName
- * @return
+ * @deprecated this is a back door for xbeans. do not use, will
+ * be removed soon.
*/
- public MAnnotation addAnnotationForTag(String tagName, String value) {
- MAnnotation ann = findOrCreateAnnotation(tagName);
- JClass type = getClassLoader().loadClass("java.lang.String");
- ann.setSimpleValue(JAnnotation.SINGLE_VALUE_NAME,value,type);
- MAnnotation litann = addLiteralAnnotation(tagName);
- litann.setSimpleValue(JAnnotation.SINGLE_VALUE_NAME,value,type);
- return ann;
- }
-
- /*
- private String getAnnotationTypeFor(/*Annotation Object annotationInstance) {
- //FIXME this may be broken, not sure yet what the class of an annotation
- // instance is. we may need to climb the type tree.
- return annotationInstance.getClass().getName();
- }
-
- public MAnnotation addAnnotationForInstance(Class annType,
- Object instance) {
- if (annType == null) throw new IllegalArgumentException("null anntype");
- if (instance == null) throw new IllegalArgumentException("null instance");
- MAnnotation ann = getMutableAnnotation(annType);
- if (ann != null) {
- ann.setAnnotationInstance(instance);
- ((AnnotationProxy)ann.getProxy()).initFromAnnotationInstance(annType,instance);//REVIEW not totally comfortable with this cast
- //REVIEW this is a weird case where they add another instance
- // of the same annotation type. We'll just go with it for now,
- // but we might want to throw an exception here, not sure.
- } else {
- AnnotationProxy proxy = getContext().createProxyForAnnotationType
- (getAnnotationTypeFor(instance));
- proxy.initFromAnnotationInstance(annType,instance);
- ann = new AnnotationImpl(getContext(),proxy,annType.getName());
- ann.setAnnotationInstance(instance);
- setArtifact(instance);
- addAnnotation(ann);
+ public MAnnotation addAnnotationForProxy(Class proxyClass,
+ AnnotationProxy proxy)
+ {
+ //ClassImpl.validateClassName(annotationName);
+ String annotationName = proxyClass.getName();
+ MAnnotation ann = getMutableAnnotation(annotationName);
+ if (ann != null) return ann;
+ ann = new AnnotationImpl(getContext(),proxy,annotationName);
+ if (mName2Annotation == null) {
+ mName2Annotation = new HashMap();
}
+ mName2Annotation.put(ann.getSimpleName(),ann);
return ann;
- }*/
+ }
}
1.29 +2 -2 xml-xmlbeans/v2/jam/src/org/apache/xmlbeans/impl/jam/internal/javadoc/JavadocClassBuilder.java
Index: JavadocClassBuilder.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/jam/src/org/apache/xmlbeans/impl/jam/internal/javadoc/JavadocClassBuilder.java,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- JavadocClassBuilder.java 22 Jun 2004 21:45:57 -0000 1.28
+++ JavadocClassBuilder.java 24 Jun 2004 05:30:00 -0000 1.29
@@ -69,8 +69,8 @@
mLogger.verbose("mParseTags="+mParseTags,this);
}
try {
- // class for name this because it's 1.5 specific. if it fails, we
- // don't want to use the extractor
+ // See if we can load a 1.5-specific class. If we can't, don't use
+ // the Javadoc15Delegate.
Class.forName("com.sun.javadoc.AnnotationDesc");
} catch (ClassNotFoundException e) {
if (mLogger.isVerbose(this)) {
1.15 +5 -38 xml-xmlbeans/v2/jam/src/org/apache/xmlbeans/impl/jam/internal/reflect/ReflectClassBuilder.java
Index: ReflectClassBuilder.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/jam/src/org/apache/xmlbeans/impl/jam/internal/reflect/ReflectClassBuilder.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- ReflectClassBuilder.java 23 Jun 2004 00:53:18 -0000 1.14
+++ ReflectClassBuilder.java 24 Jun 2004 05:30:00 -0000 1.15
@@ -34,7 +34,7 @@
// ========================================================================
// Constants
- private static final String JAVA15_EXTRACTOR =
+ private static final String JAVA15_DELEGATE =
"org.apache.xmlbeans.impl.jam.internal.java15.Reflect15DelegateImpl";
// ========================================================================
@@ -74,20 +74,20 @@
// ok, if we could load that, let's new up the extractor delegate
try {
mDelegate = (Reflect15Delegate)
- Class.forName(JAVA15_EXTRACTOR).newInstance();
+ Class.forName(JAVA15_DELEGATE).newInstance();
mDelegate.init((ElementContext)ctx);
// if this fails for any reason, things are in a bad state
} catch (ClassNotFoundException e) {
mLogger.error("Internal error, failed to instantiate "+
- JAVA15_EXTRACTOR);
+ JAVA15_DELEGATE);
mLogger.error(e);
} catch (IllegalAccessException e) {
mLogger.error("Internal error, failed to instantiate "+
- JAVA15_EXTRACTOR);
+ JAVA15_DELEGATE);
mLogger.error(e);
} catch (InstantiationException e) {
mLogger.error("Internal error, failed to instantiate "+
- JAVA15_EXTRACTOR);
+ JAVA15_DELEGATE);
mLogger.error(e);
}
}
@@ -221,37 +221,4 @@
p.setType(paramType.getName());
return p;
}
-
-
-
-
-//salvaged from RClassLoader, may be useful for parser
- /*
- private void validateClassName(String className)
- throws IllegalArgumentException
- {
- if (!Character.isJavaIdentifierStart(className.charAt(0))) {
- throw new IllegalArgumentException
- ("Invalid first character in class name: "+className);
- }
- for(int i=1; i<className.length(); i++) {
- char c = className.charAt(i);
- if (c == '.') {
- if (className.charAt(i-1) == '.') {
- throw new IllegalArgumentException
- ("'..' not allowed in class name: "+className);
- }
- if (i == className.length()-1) {
- throw new IllegalArgumentException
- ("'.' not allowed at end of class name: "+className);
- }
- } else {
- if (!Character.isJavaIdentifierPart(c)) {
- throw new IllegalArgumentException
- ("Illegal character '"+c+"' in class name: "+className);
- }
- }
- }
- }
- */
}
1.7 +0 -6 xml-xmlbeans/v2/jam/src/org/apache/xmlbeans/impl/jam/mutable/MAnnotatedElement.java
Index: MAnnotatedElement.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/jam/src/org/apache/xmlbeans/impl/jam/mutable/MAnnotatedElement.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- MAnnotatedElement.java 28 May 2004 17:02:51 -0000 1.6
+++ MAnnotatedElement.java 24 Jun 2004 05:30:00 -0000 1.7
@@ -43,10 +43,4 @@
//DOCME
public void removeComment();
-
-
- /**
- * @deprecated
- */
- public MAnnotation addAnnotationForTag(String tagName, String value);
}
1.53 +83 -131 xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Java2Schema.java
Index: Java2Schema.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Java2Schema.java,v
retrieving revision 1.52
retrieving revision 1.53
diff -u -r1.52 -r1.53
--- Java2Schema.java 22 Apr 2004 17:27:06 -0000 1.52
+++ Java2Schema.java 24 Jun 2004 05:30:00 -0000 1.53
@@ -19,6 +19,7 @@
import org.apache.xmlbeans.impl.binding.tylar.TylarWriter;
import org.apache.xmlbeans.impl.binding.tylar.TylarConstants;
import org.apache.xmlbeans.impl.binding.tylar.ExplodedTylarImpl;
+import org.apache.xmlbeans.impl.binding.compile.internal.Java2SchemaAnnotationHelper;
import org.apache.xmlbeans.impl.jam.*;
import org.apache.xmlbeans.impl.common.XMLChar;
import org.apache.xmlbeans.SchemaTypeSystem;
@@ -31,6 +32,8 @@
import java.util.HashMap;
import java.util.Collection;
import java.util.Iterator;
+import java.util.Set;
+import java.util.HashSet;
import java.io.IOException;
import java.io.StringWriter;
@@ -48,10 +51,7 @@
// =========================================================================
// Constants
- private static final String JAVA_URI_SCHEME = "java:";
- private static final String JAVA_NAMESPACE_URI = "language_builtins";
- private static final String JAVA_PACKAGE_PREFIX = "java.";
-
+ //FIXME these should be hidden away down in Java2SchemaAnnotationHelper
public static final String TAG_CT = "xsdgen:complexType";
public static final String TAG_CT_EXCLUDE = TAG_CT+".exclude";
public static final String TAG_CT_TYPENAME = TAG_CT+".typeName";
@@ -90,6 +90,12 @@
// the input classes
private JClass[] mClasses;
+ private Java2SchemaAnnotationHelper mAnnHelper =
+ Java2SchemaAnnotationHelper.getInstance();
+
+ // Set of JClasses for which element annotations have already been processed.
+ private Set mCheckForElements = new HashSet();
+
// =========================================================================
// Constructors
@@ -111,10 +117,16 @@
mBindingFile = new BindingFile();
mLoader = CompositeBindingLoader.forPath
(new BindingLoader[] {mBindingFile, super.getBaseBindingLoader()});
- //This does the binding
- for(int i=0; i<mClasses.length; i++) {
- if (!getAnnotation(mClasses[i],TAG_CT_EXCLUDE,false)) {
- getBindingTypeFor(mClasses[i]);
+ { // generate/bind types
+ for(int i=0; i<mClasses.length; i++) {
+ if (!mAnnHelper.isExclude(mClasses[i])) {
+ getBindingTypeFor(mClasses[i]);
+ }
+ }
+ }
+ { // generate/bind elements
+ for(Iterator i = mCheckForElements.iterator(); i.hasNext(); ) {
+ ensureElementsExistFor((JClass)i.next());
}
}
//
@@ -171,7 +183,6 @@
return out;
}
-
/**
* Returns a bts BindingType for the given JClass. If such a type
* has not yet been registered with the loader, it will be created.
@@ -179,6 +190,8 @@
* @param clazz Java type for which to return a binding.
*/
private BindingType getBindingTypeFor(JClass clazz) {
+ if (clazz == null) throw new IllegalArgumentException("null clazz");
+ mCheckForElements.add(clazz);
BindingTypeName btn = mLoader.lookupTypeFor(getJavaName(clazz));
if (btn != null) {
BindingType out = mLoader.getBindingType(btn);
@@ -187,6 +200,47 @@
return createBindingTypeFor(clazz);
}
+ private void ensureElementsExistFor(JClass clazz) {
+ if (clazz == null) throw new IllegalArgumentException("null clazz");
+ BindingTypeName btn = mLoader.lookupTypeFor(JavaTypeName.forJClass(clazz));
+ if (btn == null) {
+ logWarning("No bindings produced for "+clazz.getQualifiedName()+", " +
+ "skipping element generation");
+ return;
+ }
+ QName typeQName = btn.getXmlName().getQName();
+ QName[] tes = mAnnHelper.getTargetElements(clazz);
+ if (tes != null) {
+ for(int i=0; i<tes.length; i++ ) {
+ logVerbose("creating target element "+tes[i]);
+ // create an appropriate schema element, if appropriate
+ SchemaDocument.Schema destSchema =
+ findOrCreateSchema(tes[i].getNamespaceURI());
+ if (!containsElementNamed(destSchema, tes[i].getLocalPart())) {
+ TopLevelElement elem = destSchema.addNewElement();
+ elem.setName(tes[i].getLocalPart());
+ elem.setType(typeQName);
+ }
+ // create a binding entry
+ BindingTypeName docBtName = BindingTypeName.forPair
+ (getJavaName(clazz),
+ XmlTypeName.forGlobalName(XmlTypeName.ELEMENT, tes[i]));
+ SimpleDocumentBinding sdb = new SimpleDocumentBinding(docBtName);
+ sdb.setTypeOfElement(XmlTypeName.forTypeNamed(typeQName));
+ mBindingFile.addBindingType(sdb,false,true);
+ }
+ }
+ }
+
+ private boolean containsElementNamed(SchemaDocument.Schema xsd, String elementName) {
+ // this is fairly gross, ma
+ TopLevelElement[] es = xsd.getElementArray();
+ for(int i=0; i<es.length; i++) {
+ if (elementName.equals(es[i].getName())) return true;
+ }
+ return false;
+ }
+
/**
* Creates a bts BindingType for the given JClass and registers t with the
* loader. Note that this method assumes that a BindingType does not
@@ -195,13 +249,12 @@
* @param clazz Java type for which to generate a binding.
*/
private BindingType createBindingTypeFor(JClass clazz) {
+ logVerbose("** creating binding type for "+clazz.getQualifiedName()+" "+clazz.isPrimitiveType());
// create the schema type
- SchemaDocument.Schema schema = findOrCreateSchema(getTargetNamespace(clazz));
+ QName targetQname = mAnnHelper.getTargetTypeName(clazz);
+ SchemaDocument.Schema schema = findOrCreateSchema(targetQname.getNamespaceURI());
TopLevelComplexType xsType = schema.addNewComplexType();
- String tns = getTargetNamespace(clazz);
- String xsdName = getAnnotation(clazz,TAG_CT_TYPENAME,clazz.getSimpleName());
- QName qname = new QName(tns,xsdName);
- xsType.setName(xsdName);
+ xsType.setName(targetQname.getLocalPart());
// deal with inheritance - see if it extends anything
JClass superclass = clazz.getSuperclass();
// we have to remember whether we created an ExtensionType because that
@@ -212,7 +265,7 @@
ExtensionType extType = null;
BindingType superBindingType = null;
if (superclass != null && !superclass.isObjectType() &&
- !getAnnotation(clazz,TAG_CT_IGNORESUPER,false)) {
+ !mAnnHelper.isIgnoreSuper(clazz)) {
// FIXME we're ignoring interfaces at the moment
superBindingType = getBindingTypeFor(superclass);
ComplexContentDocument.ComplexContent ccd = xsType.addNewComplexContent();
@@ -221,7 +274,7 @@
}
// create a binding type
BindingTypeName btname = BindingTypeName.forPair(getJavaName(clazz),
- XmlTypeName.forTypeNamed(qname));
+ XmlTypeName.forTypeNamed(targetQname));
ByNameBean bindType = new ByNameBean(btname);
mBindingFile.addBindingType(bindType,true,true);
if (clazz.isPrimitiveType()) {
@@ -229,7 +282,6 @@
logError("Unexpected simple type",clazz);
return bindType;
}
-
//add super's props first
if (superBindingType != null) {
//REVIEW: will it ever be possible to have another type as the super type?
@@ -243,32 +295,14 @@
bindType.setAnyAttributeProperty(super_type.getAnyAttributeProperty());
bindType.setAnyElementProperty(super_type.getAnyElementProperty());
}
-
- String rootName = getAnnotation(clazz,TAG_CT_ROOT,null);
- if (rootName != null) {
- QName rootQName = new QName(tns, rootName);
- BindingTypeName docBtName =
- BindingTypeName.forPair(getJavaName(clazz),
- XmlTypeName.forGlobalName(XmlTypeName.ELEMENT, rootQName));
- SimpleDocumentBinding sdb = new SimpleDocumentBinding(docBtName);
- sdb.setTypeOfElement(btname.getXmlName());
- mBindingFile.addBindingType(sdb,true,true);
- }
- // run through the class' properties to populate the binding and xsdtypes
- SchemaPropertyFacade facade = new SchemaPropertyFacade(xsType,extType,bindType,tns);
- Map props2issetters = new HashMap();
- getIsSetters(clazz,props2issetters);
- bindProperties(clazz.getDeclaredProperties(),props2issetters,facade);
- facade.finish();
- // check to see if they want to create a root elements from this type
- JAnnotation[] anns = getNamedTags(clazz.getAllJavadocTags(),TAG_CT_ROOT);
- for(int i=0; i<anns.length; i++) {
- TopLevelElement root = schema.addNewElement();
- root.setName(makeNcNameSafe(anns[i].getValue
- (JAnnotation.SINGLE_VALUE_NAME).asString()));
- root.setType(qname);
- // FIXME still not entirely clear to me what we should do about
- // the binding file here
+ {
+ // run through the class' properties to populate the binding and xsdtypes
+ SchemaPropertyFacade facade = new SchemaPropertyFacade
+ (xsType,extType,bindType,targetQname.getNamespaceURI());
+ Map props2issetters = new HashMap();
+ getIsSetters(clazz,props2issetters);
+ bindProperties(clazz.getDeclaredProperties(),props2issetters,facade);
+ facade.finish();
}
return bindType;
}
@@ -315,7 +349,7 @@
Map props2issetters,
SchemaPropertyFacade facade) {
for(int i=0; i<props.length; i++) {
- if (getAnnotation(props[i],TAG_EL_EXCLUDE,false)) {
+ if (mAnnHelper.getAnnotation(props[i],TAG_EL_EXCLUDE,false)) {
logVerbose("Marked excluded, skipping",props[i]);
continue;
}
@@ -325,20 +359,19 @@
}
String propName;
{ // determine the property name to use and set it
- propName = getAnnotation(props[i], TAG_AT_NAME, null);
+ propName = mAnnHelper.getAnnotation(props[i], TAG_AT_NAME, null);
if (propName != null) {
facade.newAttributeProperty(props[i]);
} else {
facade.newElementProperty(props[i]);
- propName = getAnnotation(props[i], TAG_EL_NAME,
- props[i].getSimpleName());
+ propName = mAnnHelper.getAnnotation(props[i], TAG_EL_NAME,
+ props[i].getSimpleName());
}
- assert propName != null;
facade.setSchemaName(propName);
}
{ // determine the property type to use and set it
JClass propType = null;
- String annotatedType = getAnnotation(props[i],TAG_EL_ASTYPE,null);
+ String annotatedType = mAnnHelper.getAnnotation(props[i],TAG_EL_ASTYPE,null);
if (annotatedType == null) {
facade.setType(propType = props[i].getType());
} else {
@@ -389,65 +422,6 @@
}
/**
- * Returns the string value of a named annotation, or the provided default
- * if the annotation is not present.
- * REVIEW seems like having this functionality in jam_old would be nice
- */
- private String getAnnotation(JAnnotatedElement elem,
- String annName,
- String dflt) {
- //System.out.print("checking for "+annName+" on "+elem.getQualifiedName());
- JAnnotation ann = getAnnotation(elem,annName);
- if (ann == null) {
- //System.out.println("...no annotation");
- return dflt;
- }
- JAnnotationValue val = ann.getValue(JAnnotation.SINGLE_VALUE_NAME);
- if (val == null) {
- //System.out.println("...no value!!!");
- return dflt;
- }
- //System.out.println("\n\n\n...value of "+annName+" is "+val.asString()+"!!!!!!!!!");
- return val.asString();
- }
-
- /**
- * Returns the boolean value of a named annotation, or the provided default
- * if the annotation is not present.
- * REVIEW seems like having this functionality in jam_old would be nice
- */
- private boolean getAnnotation(JAnnotatedElement elem,
- String annName,
- boolean dflt) {
- //System.out.print("checking for "+annName+" on "+elem.getQualifiedName());
- JAnnotation ann = getAnnotation(elem,annName);
- if (ann == null) {
- //System.out.println("...no annotation");
- return dflt;
- }
- JAnnotationValue val = ann.getValue(JAnnotation.SINGLE_VALUE_NAME);
- if (val == null || val.asString().length() == 0) {
- //System.out.println("\n\n\n...no value, returning true!!!");
- //this is a little bit gross. the logic here is that if the tag is
- //present but empty, it actually is a true value. E.g., an empty
- //@exclude tag means "yes, do exclude."
- return true;
- }
- //System.out.println("\n\n\n...value of "+annName+" is "+val.asBoolean()+"!!!!!!!!!");
- return val.asBoolean();
- }
-
- //FIXME this is temporary until we get the tags/175 sorted out
- private JAnnotation getAnnotation(JAnnotatedElement e,
- String named) {
- JAnnotation[] tags = e.getAllJavadocTags();
- for(int i=0; i<tags.length; i++) {
- if (tags[i].getSimpleName().equals(named)) return tags[i];
- }
- return null;
- }
-
- /**
* Returns a QName for the type bound to the given JClass.
*/
private QName getQnameFor(JClass clazz) {
@@ -461,29 +435,6 @@
return new QName("ERROR",clazz.getQualifiedName());
}
- /**
- * Returns a target namespace that should be used for the given class.
- * This takes annotations into consideration.
- */
- private String getTargetNamespace(JClass clazz) {
- String val = getAnnotation(clazz,TAG_CT_TARGETNS,null);
- if (val != null) return val;
- // Ok, they didn't specify it in the markup, so we have to
- // synthesize it from the classname.
- String pkg_name;
- if (clazz.isPrimitiveType()) {
- pkg_name = JAVA_NAMESPACE_URI;
- } else {
- JPackage pkg = clazz.getContainingPackage();
- pkg_name = (pkg == null) ? "" : pkg.getQualifiedName();
- if (pkg_name.startsWith(JAVA_PACKAGE_PREFIX)) {
- pkg_name = JAVA_NAMESPACE_URI+'.'+
- pkg_name.substring(JAVA_PACKAGE_PREFIX.length());
- }
- }
- return JAVA_URI_SCHEME + pkg_name;
- }
-
/**
@@ -632,6 +583,7 @@
* generated schema.
*/
public void setSchemaName(String name) {
+ if (name == null) throw new IllegalArgumentException("null name");
name = makeNcNameSafe(name);
if (mXsElement != null) {
mXsElement.setName(name);
1.1 xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/internal/Java2SchemaAnnotationHelper.java
Index: Java2SchemaAnnotationHelper.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* 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.apache.xmlbeans.impl.binding.compile.internal;
import org.apache.xmlbeans.impl.jam.JClass;
import org.apache.xmlbeans.impl.jam.JPackage;
import org.apache.xmlbeans.impl.jam.JAnnotatedElement;
import org.apache.xmlbeans.impl.jam.JAnnotation;
import org.apache.xmlbeans.impl.jam.JAnnotationValue;
import org.apache.xmlbeans.impl.binding.compile.internal.annotations.ClassBindingInfo;
import org.apache.xmlbeans.impl.binding.compile.internal.annotations.TypeTarget;
import org.apache.xmlbeans.impl.binding.compile.internal.annotations.ElementTarget;
import org.apache.xmlbeans.impl.binding.compile.Java2Schema;
import javax.xml.namespace.QName;
import java.util.List;
import java.util.ArrayList;
/**
* @author Patrick Calahan <email: pcal-at-bea-dot-com>
*/
public class Java2SchemaAnnotationHelper {
// ========================================================================
// Singleton
public static Java2SchemaAnnotationHelper getInstance() { return INSTANCE; }
private static Java2SchemaAnnotationHelper INSTANCE = new Java2SchemaAnnotationHelper();
private Java2SchemaAnnotationHelper() {}
// ========================================================================
// Constants
private static final String JAVA_URI_SCHEME = "java:";
private static final String JAVA_NAMESPACE_URI = "language_builtins";
//private static final String JAVA_PACKAGE_PREFIX = "java.";
// ========================================================================
// Public methods
public QName getTargetTypeName(JClass clazz) {
String tns = getTargetNamespace(clazz);
String local = getTargetLocalName(clazz);
return new QName(tns,local);
}
public QName[] getTargetElements(JClass clazz) {
List list = null;
{ // old school
String rootName = getAnnotation(clazz,Java2Schema.TAG_CT_ROOT,null);
if (rootName != null) {
list = new ArrayList();
list.add(new QName(getTargetNamespace(clazz), rootName));
}
}
{ // new school
ClassBindingInfo info =
(ClassBindingInfo)clazz.getAnnotationProxy(ClassBindingInfo.class);
if (info != null) {
TypeTarget tt = info.getDefaultTargetType();
if (tt != null) {
ElementTarget[] e = tt.getTopLevelElements();
if (e != null && e.length > 0) {
if (list == null) list = new ArrayList();
for(int i=0; i<e.length; i++) {
list.add(new QName(e[i].getNamespaceUri(),e[i].getLocalName()));
}
}
}
}
}
if (list == null) return null;
QName[] out = new QName[list.size()];
list.toArray(out);
return out;
}
public boolean isExclude(JClass clazz) {
if (getAnnotation(clazz,Java2Schema.TAG_CT_EXCLUDE,false)) return true;
ClassBindingInfo info =
(ClassBindingInfo)clazz.getAnnotationProxy(ClassBindingInfo.class);
return (info == null) ? false : info.isExclude();
}
public boolean isIgnoreSuper(JClass clazz) {
if (getAnnotation(clazz,Java2Schema.TAG_CT_IGNORESUPER,false)) return true;
ClassBindingInfo info =
(ClassBindingInfo)clazz.getAnnotationProxy(ClassBindingInfo.class);
if (info == null) return false;
TypeTarget tt = info.getDefaultTargetType();
return (tt == null) ? false : tt.isIgnoreJavaInheritance();
}
/**
* Returns the string value of a named annotation, or the provided default
* if the annotation is not present.
* REVIEW seems like having this functionality in jam would be nice
*/
public String getAnnotation(JAnnotatedElement elem,
String annName,
String dflt)
{
//System.out.print("checking for "+annName+" on "+elem.getQualifiedName());
JAnnotation ann = getAnnotation(elem,annName);
if (ann == null) {
//System.out.println("...no annotation");
return dflt;
}
JAnnotationValue val = ann.getValue(JAnnotation.SINGLE_VALUE_NAME);
if (val == null) {
//System.out.println("...no value!!!");
return dflt;
}
//System.out.println("\n\n\n...value of "+annName+" is "+val.asString()+"!!!!!!!!!");
return val.asString();
}
/**
* Returns the boolean value of a named annotation, or the provided default
* if the annotation is not present.
* REVIEW seems like having this functionality in jam would be nice
*/
public boolean getAnnotation(JAnnotatedElement elem,
String annName,
boolean dflt)
{
//System.out.print("checking for "+annName+" on "+elem.getQualifiedName());
JAnnotation ann = getAnnotation(elem,annName);
if (ann == null) {
//System.out.println("...no annotation");
return dflt;
}
JAnnotationValue val = ann.getValue(JAnnotation.SINGLE_VALUE_NAME);
if (val == null || val.asString().length() == 0) {
//System.out.println("\n\n\n...no value, returning true!!!");
//this is a little bit gross. the logic here is that if the tag is
//present but empty, it actually is a true value. E.g., an empty
//@exclude tag means "yes, do exclude."
return true;
}
//System.out.println("\n\n\n...value of "+annName+" is "+val.asBoolean()+"!!!!!!!!!");
return val.asBoolean();
}
//FIXME this is temporary until we get the tags/175 sorted out
public JAnnotation getAnnotation(JAnnotatedElement e, String named) {
JAnnotation[] tags = e.getAllJavadocTags();
for(int i=0; i<tags.length; i++) {
if (tags[i].getSimpleName().equals(named)) return tags[i];
}
return null;
}
// ========================================================================
// Private methods
private String getTargetLocalName(JClass clazz) {
String name = getAnnotation(clazz,Java2Schema.TAG_CT_TYPENAME,null);
if (name != null) return name;
ClassBindingInfo info =
(ClassBindingInfo)clazz.getAnnotationProxy(ClassBindingInfo.class);
if (info != null) {
TypeTarget tt = info.getDefaultTargetType();
name = tt.getLocalName();
if (name != null) return name;
}
return clazz.getSimpleName();
}
/**
* Returns a target namespace that should be used for the given class.
* This takes annotations into consideration.
*/
private String getTargetNamespace(JClass clazz) {
String val = getAnnotation(clazz,Java2Schema.TAG_CT_TARGETNS,null);
if (val != null) {
return val;
}
ClassBindingInfo info =
(ClassBindingInfo)clazz.getAnnotationProxy(ClassBindingInfo.class);
if (info != null) {
TypeTarget tt = info.getDefaultTargetType();
val = tt.getNamespaceUri();
if (val != null) return val;
}
// Ok, they didn't specify it in the markup, so we have to
// synthesize it from the classname.
String pkg_name;
if (clazz.isPrimitiveType()) {
pkg_name = JAVA_NAMESPACE_URI;
} else {
JPackage pkg = clazz.getContainingPackage();
pkg_name = (pkg == null) ? "" : pkg.getQualifiedName();
/*if (pkg_name.startsWith(JAVA_PACKAGE_PREFIX)) {
pkg_name = JAVA_NAMESPACE_URI+'.'+
pkg_name.substring(JAVA_PACKAGE_PREFIX.length());
}*/
}
return JAVA_URI_SCHEME + pkg_name;
}
}
1.1 xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/internal/annotations/ClassBindingInfo.java
Index: ClassBindingInfo.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* 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.apache.xmlbeans.impl.binding.compile.internal.annotations;
import org.apache.xmlbeans.impl.jam.annotation.TypedAnnotationProxyBase;
/**
* @author Patrick Calahan <email: pcal-at-bea-dot-com>
*/
public class ClassBindingInfo extends TypedAnnotationProxyBase {
// ========================================================================
// Variables
private TypeTarget mDefaultSchemaTypeTarget = null;
private boolean mExclude = false;
// ========================================================================
// Public methods
public TypeTarget getDefaultTargetType() {
return mDefaultSchemaTypeTarget;
}
public boolean isExclude() { return mExclude; }
// ========================================================================
// Mutators
public void setDefaultTargetType(TypeTarget stt) {
mDefaultSchemaTypeTarget = stt;
}
public void setExclude(boolean b) { mExclude = b; }
}
1.1 xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/internal/annotations/ElementTarget.java
Index: ElementTarget.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* 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.apache.xmlbeans.impl.binding.compile.internal.annotations;
import javax.xml.namespace.QName;
/**
* @author Patrick Calahan <email: pcal-at-bea-dot-com>
*/
public class ElementTarget extends NamedTarget {
// ========================================================================
// Constructors
public ElementTarget(QName qn) { super(qn); }
public ElementTarget(String ns, String local) { super(ns,local); }
}
1.1 xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/internal/annotations/NamedTarget.java
Index: NamedTarget.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* 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.apache.xmlbeans.impl.binding.compile.internal.annotations;
import org.apache.xmlbeans.impl.jam.annotation.TypedAnnotationProxyBase;
import javax.xml.namespace.QName;
/**
* @author Patrick Calahan <email: pcal-at-bea-dot-com>
*/
public abstract class NamedTarget extends TypedAnnotationProxyBase {
// ========================================================================
// Variables
private String mLocalName;
private String mNamespace;
// ========================================================================
// Constructors
protected NamedTarget() {}
protected NamedTarget(QName qn) {
if (qn == null) throw new IllegalArgumentException("null qname");
mLocalName = qn.getLocalPart();
mNamespace = qn.getNamespaceURI();
}
protected NamedTarget(String ns, String local) {
if (ns == null) throw new IllegalArgumentException("null namespace");
if (local == null) throw new IllegalArgumentException("null local name");
mLocalName = local;
mNamespace = ns;
}
// ========================================================================
// Accessors
public String getLocalName() { return mLocalName; }
public String getNamespaceUri() { return mNamespace; }
}
1.1 xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/internal/annotations/TypeTarget.java
Index: TypeTarget.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* 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.apache.xmlbeans.impl.binding.compile.internal.annotations;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.List;
/**
* @author Patrick Calahan <email: pcal-at-bea-dot-com>
*/
public class TypeTarget extends NamedTarget {
// ========================================================================
// Variables
private List mTargetElements = null;
private boolean mIgnoreInheritance = false;
// ========================================================================
// Constructors
// type targets don't have to have a name - that means they want us to
// generate one
public TypeTarget() {}
public TypeTarget(QName qn) {
super(qn);
}
public TypeTarget(String ns, String local) {
super(ns,local);
}
// ========================================================================
// Accessors
public ElementTarget[] getTopLevelElements() {
if (mTargetElements == null) return null;
ElementTarget[] out = new ElementTarget[mTargetElements.size()];
mTargetElements.toArray(out);
return out;
}
public boolean isIgnoreJavaInheritance() { return mIgnoreInheritance; }
// ========================================================================
// Mutators
public void addNewTopLevelElement(QName qn) {
if (mTargetElements == null) mTargetElements = new ArrayList();
mTargetElements.add(new ElementTarget(qn));
}
public void setIgnoreJavaInheritance(boolean b) { mIgnoreInheritance = b; }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org