Author: nbelaevski
Date: 2009-12-23 18:04:15 -0500 (Wed, 23 Dec 2009)
New Revision: 16208
Removed:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/builder/render/
Modified:
root/cdk/trunk/plugins/attributes/
root/cdk/trunk/plugins/attributes/src/main/java/org/richfaces/cdk/attributes/Attribute.java
root/cdk/trunk/plugins/attributes/src/test/java/org/richfaces/cdk/attributes/AttributesTest.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/types/TypesFactory.java
root/cdk/trunk/plugins/generator/src/main/script/SchemaProcessor.groovy
Log:
Refactoring: added Attribute.Kind annotation + handling
Removed legacy renderer for .java code
Fixed wrong messages in TypesFactory
Property changes on: root/cdk/trunk/plugins/attributes
___________________________________________________________________
Name: svn:ignore
- .classpath
.project
.settings
target
+ .classpath
.project
.settings
target
.clover
Modified:
root/cdk/trunk/plugins/attributes/src/main/java/org/richfaces/cdk/attributes/Attribute.java
===================================================================
---
root/cdk/trunk/plugins/attributes/src/main/java/org/richfaces/cdk/attributes/Attribute.java 2009-12-23
19:00:19 UTC (rev 16207)
+++
root/cdk/trunk/plugins/attributes/src/main/java/org/richfaces/cdk/attributes/Attribute.java 2009-12-23
23:04:15 UTC (rev 16208)
@@ -34,6 +34,10 @@
*/
public class Attribute implements KeyedType {
+ public enum Kind {
+ GENERIC, BOOLEAN, URI
+ }
+
@XmlRootElement(name = "attributes")
@XmlType(name = "AttributesType")
public static final class Type implements ContainerType<Attribute> {
@@ -57,8 +61,8 @@
private String defaultValue;
- private boolean uri;
-
+ private Kind kind = Kind.GENERIC;
+
public Attribute() {
super();
}
@@ -119,18 +123,18 @@
}
/**
- * @return the uri
+ * @return the kind
*/
@XmlElement
- public boolean isUri() {
- return uri;
+ public Kind getKind() {
+ return kind;
}
-
+
/**
- * @param uri the uri to set
+ * @param kind the kind to set
*/
- public void setUri(boolean uri) {
- this.uri = uri;
+ public void setKind(Kind kind) {
+ this.kind = kind;
}
/* (non-Javadoc)
Modified:
root/cdk/trunk/plugins/attributes/src/test/java/org/richfaces/cdk/attributes/AttributesTest.java
===================================================================
---
root/cdk/trunk/plugins/attributes/src/test/java/org/richfaces/cdk/attributes/AttributesTest.java 2009-12-23
19:00:19 UTC (rev 16207)
+++
root/cdk/trunk/plugins/attributes/src/test/java/org/richfaces/cdk/attributes/AttributesTest.java 2009-12-23
23:04:15 UTC (rev 16208)
@@ -21,42 +21,139 @@
*/
package org.richfaces.cdk.attributes;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
import java.io.StringReader;
import java.io.StringWriter;
+import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.transform.stream.StreamResult;
+import org.junit.Before;
import org.junit.Test;
+import org.richfaces.cdk.attributes.Attribute.Kind;
-
/**
* @author Nick Belaevski
*
*/
public class AttributesTest {
- @Test
- public void testStub() throws Exception {
+ private SchemaSet schemaSet;
+
+ private String marshal(SchemaSet schemaSet) throws Exception {
+ StringWriter writer = new StringWriter();
JAXBContext jc = JAXBContext.newInstance(SchemaSet.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+ marshaller.marshal(schemaSet, new StreamResult(writer));
+
+ return writer.toString();
+ }
+
+ private SchemaSet unmarshal(String xmlData) throws Exception {
+ StringReader reader = new StringReader(xmlData);
+ JAXBContext jc = JAXBContext.newInstance(SchemaSet.class);
+ Unmarshaller unmarshaller = jc.createUnmarshaller();
- SchemaSet schemaSet = new SchemaSet();
- Schema schema = new Schema("abc");
+ return (SchemaSet) unmarshaller.unmarshal(reader);
+ }
+
+ @Before
+ public void setUp() {
+ schemaSet = new SchemaSet();
+ Schema schema = new Schema("urn:abc");
schemaSet.addSchema(schema);
- Element element = new Element("ele");
- schema.addElement(element);
- Attribute attribute = new Attribute("attr");
- element.addAttribute(attribute);
- StringWriter writer = new StringWriter();
- marshaller.marshal(schemaSet, writer);
+ Element spanElement = new Element("span");
+ schema.addElement(spanElement);
+
+ Attribute idAttribute = new Attribute("id");
+ spanElement.addAttribute(idAttribute);
- System.out.println(writer.toString());
+ Element imgElement = new Element("img");
+ schema.addElement(imgElement);
+
+ Attribute altAttribute = new Attribute("alt");
+ altAttribute.setRequired(true);
+ imgElement.addAttribute(altAttribute);
- jc.createUnmarshaller().unmarshal(new StringReader(writer.toString()));
+ Attribute srcAttribute = new Attribute("src");
+ srcAttribute.setKind(Kind.URI);
+ imgElement.addAttribute(srcAttribute);
+
+ Attribute dirAttribute = new Attribute("dir");
+ dirAttribute.setDefaultValue("ltr");
+ imgElement.addAttribute(dirAttribute);
}
+
+ public void tearDown() {
+ schemaSet = null;
+ }
+
+ @Test
+ public void testMarshalUnmarshal() throws Exception {
+ String marshalledData = marshal(schemaSet);
+
+ System.out.println("AttributesTest.testMarshalUnmarshal() " +
marshalledData);
+
+ assertNotNull(marshalledData);
+ assertTrue(marshalledData.length() > 0);
+
+ SchemaSet restoredSchemaSet = unmarshal(marshalledData);
+ assertNotNull(restoredSchemaSet);
+
+ assertEquals(1, restoredSchemaSet.getSchemas().size());
+ Schema restoredSchema = restoredSchemaSet.getSchemas().get("urn:abc");
+ assertEquals("urn:abc", restoredSchema.getNamespace());
+ assertEquals("urn:abc", restoredSchema.getKey());
+
+ Map<String, Element> restoredElements = restoredSchema.getElements();
+ assertEquals(2, restoredElements.size());
+
+ Element restoredSpanElement = restoredElements.get("span");
+ assertNotNull(restoredSpanElement);
+ assertEquals("span", restoredSpanElement.getName());
+ assertEquals("span", restoredSpanElement.getKey());
+ Map<String, Attribute> restoredSpanAttributes =
restoredSpanElement.getAttributes();
+ assertEquals(1, restoredSpanAttributes.size());
+ Attribute restoredIdAttribute = restoredSpanAttributes.get("id");
+ assertNotNull(restoredIdAttribute);
+ assertEquals("id", restoredIdAttribute.getName());
+ assertEquals("id", restoredIdAttribute.getKey());
+ assertNull(restoredIdAttribute.getDefaultValue());
+ assertFalse(restoredIdAttribute.isRequired());
+ assertEquals(Kind.GENERIC, restoredIdAttribute.getKind());
+
+ Element restoredImgElement = restoredElements.get("img");
+ assertNotNull(restoredImgElement);
+ assertEquals("img", restoredImgElement.getName());
+ assertEquals("img", restoredImgElement.getKey());
+ Map<String, Attribute> restoredImgAttributes =
restoredImgElement.getAttributes();
+ assertEquals(3, restoredImgAttributes.size());
+
+ Attribute restoredAltAttribute = restoredImgAttributes.get("alt");
+ assertNotNull(restoredAltAttribute);
+ assertEquals("alt", restoredAltAttribute.getName());
+ assertTrue(restoredAltAttribute.isRequired());
+
+ Attribute restoredSrcAttribute = restoredImgAttributes.get("src");
+ assertNotNull(restoredSrcAttribute);
+ assertEquals("src", restoredSrcAttribute.getName());
+ assertEquals(Kind.URI, restoredSrcAttribute.getKind());
+
+ Attribute restoredDirAttribute = restoredImgAttributes.get("dir");
+ assertNotNull(restoredDirAttribute);
+ assertEquals("dir", restoredDirAttribute.getName());
+ assertEquals("ltr", restoredDirAttribute.getDefaultValue());
+ }
}
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/types/TypesFactory.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/types/TypesFactory.java 2009-12-23
19:00:19 UTC (rev 16207)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/parser/el/types/TypesFactory.java 2009-12-23
23:04:15 UTC (rev 16208)
@@ -140,7 +140,7 @@
// ignore
} catch (LinkageError e) {
if (LOG.isInfoEnabled()) {
- LOG.info(MessageFormat.format("Class {0} couldn't be
loaded because of: {1}", guessTypeName,
+ LOG.info(MessageFormat.format("Class {0} couldn''t
be loaded because of: {1}", guessTypeName,
e.getMessage()));
}
}
@@ -152,7 +152,8 @@
try {
result = Class.forName(className, true, classLoader);
} catch (LinkageError e) {
- String errorMessage = MessageFormat.format("Class {0} couldn't
be loaded because of: {1}", className, e.getMessage());
+ String errorMessage = MessageFormat.format("Class {0}
couldn''t be loaded because of: {1}", className,
+ e.getMessage());
if (LOG.isInfoEnabled()) {
LOG.info(errorMessage);
}
Modified: root/cdk/trunk/plugins/generator/src/main/script/SchemaProcessor.groovy
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/script/SchemaProcessor.groovy 2009-12-23
19:00:19 UTC (rev 16207)
+++ root/cdk/trunk/plugins/generator/src/main/script/SchemaProcessor.groovy 2009-12-23
23:04:15 UTC (rev 16208)
@@ -15,6 +15,7 @@
import javax.xml.xpath.XPathFactory;
import org.richfaces.cdk.attributes.Attribute;
+import org.richfaces.cdk.attributes.Attribute.Kind;
import org.richfaces.cdk.attributes.Element;
import org.richfaces.cdk.attributes.Schema;
import org.richfaces.cdk.attributes.SchemaSet;
@@ -25,7 +26,9 @@
import com.sun.xml.xsom.XSAttributeUse;
import com.sun.xml.xsom.XSComplexType;
import com.sun.xml.xsom.XSElementDecl;
+import com.sun.xml.xsom.XSFacet;
import com.sun.xml.xsom.XSListSimpleType;
+import com.sun.xml.xsom.XSRestrictionSimpleType;
import com.sun.xml.xsom.XSSchema;
import com.sun.xml.xsom.XSSchemaSet;
import com.sun.xml.xsom.XSSimpleType;
@@ -80,52 +83,70 @@
return defaultValue;
}
-
- private boolean isURIType(XSSimpleType simpleType) {
- return isURIType(simpleType, new HashSet<XSType>());
+
+ private boolean isGenericKind(Attribute.Kind attributeKind) {
+ return Kind.GENERIC.equals(attributeKind);
+ }
+
+ private Attribute.Kind getAttributeKind(XSSimpleType simpleType) {
+ return getAttributeKind(simpleType, new HashSet<XSType>());
}
- private boolean isURIType(XSSimpleType simpleType, Set<XSType> processedTypes)
{
- if (simpleType == null) {
- return false;
+ private Attribute.Kind getAttributeKind(XSSimpleType simpleType, Set<XSType>
processedTypes) {
+ Attribute.Kind attributeKind = Kind.GENERIC;
+
+ if (simpleType == null) {
+ return attributeKind;
}
if (!processedTypes.add(simpleType)) {
- return false;
+ return attributeKind;
}
if (simpleType.isDerivedFrom(anyURIType)) {
- return true;
+ return Attribute.Kind.URI;
}
+
+ if (simpleType.isRestriction()) {
+ XSRestrictionSimpleType restrictionType = simpleType.asRestriction();
+ List<XSFacet> facets =
restrictionType.getDeclaredFacets("enumeration");
+ if (facets != null && facets.size() == 1) {
+ return Attribute.Kind.BOOLEAN;
+ }
+ }
+
+ attributeKind = getAttributeKind(simpleType.getSimpleBaseType(), processedTypes);
+ if (!isGenericKind(attributeKind)) {
+ return attributeKind;
+ }
- if (isURIType(simpleType.getSimpleBaseType(), processedTypes)) {
- return true;
- }
-
- if (isURIType(simpleType.getPrimitiveType(), processedTypes)) {
- return true;
- }
-
+ attributeKind = getAttributeKind(simpleType.getPrimitiveType(), processedTypes);
+ if (!isGenericKind(attributeKind)) {
+ return attributeKind;
+ }
+
if (simpleType.isUnion()) {
XSUnionSimpleType unionSimpleType = simpleType.asUnion();
int memberSize = unionSimpleType.getMemberSize();
for (int i = 0; i < memberSize; i++) {
XSSimpleType unionMemberType = unionSimpleType.getMember(i);
- if (isURIType(unionMemberType, processedTypes)) {
- return true;
- }
+ attributeKind = getAttributeKind(unionMemberType, processedTypes);
+ if (!isGenericKind(attributeKind)) {
+ return attributeKind;
+ }
}
} else if (simpleType.isRestriction()) {
// do nothing
} else if (simpleType.isList()) {
XSListSimpleType listSimpleType = simpleType.asList();
XSSimpleType listItemType = listSimpleType.getItemType();
- if (isURIType(listItemType, processedTypes)) {
- return true;
- }
- }
+ attributeKind = getAttributeKind(listItemType, processedTypes);
+ if (!isGenericKind(attributeKind)) {
+ return attributeKind;
+ }
+ }
- return false;
+ return attributeKind;
}
public void buildModel() throws Exception {
@@ -149,7 +170,7 @@
if (namespace.equals(attributeNamespace) ||
attributeNamespace.length() == 0) {
Attribute modelAttribute = new
Attribute(attributeDecl.getName());
modelElement.addAttribute(modelAttribute);
- modelAttribute.setUri(isURIType(attributeDecl.getType()));
+
modelAttribute.setKind(getAttributeKind(attributeDecl.getType()));
modelAttribute.setRequired(xsAttributeUse.isRequired());
modelAttribute.setDefaultValue(getDefaultValue(xsAttributeUse));
}
@@ -175,18 +196,4 @@
}
}
}
-
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- Class<?> testxsom =
SchemaProcessor.class.getClassLoader().loadClass("TestXSom");
- XSSchemaSet schemaSet = (XSSchemaSet)
testxsom.getMethod("parse2").invoke(null);
-
- SchemaProcessor testXSOM2 = new SchemaProcessor(schemaSet,
"http://richfaces.org/xhtml-el");
- testXSOM2.buildModel();
- testXSOM2.serializeModel(System.out);
- }
-
}