Author: nbelaevski
Date: 2009-08-12 11:53:08 -0400 (Wed, 12 Aug 2009)
New Revision: 15159
Added:
root/cdk/trunk/plugins/generator/src/main/script/
root/cdk/trunk/plugins/generator/src/main/script/SchemaAttributesParserTask.groovy
Log:
Groovy schema attributes generator for CDK
Added: root/cdk/trunk/plugins/generator/src/main/script/SchemaAttributesParserTask.groovy
===================================================================
--- root/cdk/trunk/plugins/generator/src/main/script/SchemaAttributesParserTask.groovy
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/main/script/SchemaAttributesParserTask.groovy 2009-08-12
15:53:08 UTC (rev 15159)
@@ -0,0 +1,94 @@
+import java.lang.Exception
+import java.io.FileOutputStream
+import java.io.*;
+import java.util.*;
+import com.sun.xml.xsom.*;
+import com.sun.xml.xsom.parser.*;
+
+/**
+ * This class reads possible attributes from schema and writes serialized map of
attributes
+ * for each element
+ */
+class SchemaParser {
+
+ Map<String, Set<String>> attributes = new HashMap<String,
Set<String>>();
+
+ private Set<String> getAttributesSet(String elementName) {
+ Set<String> attributesSet = attributes.get(elementName);
+ if (attributesSet == null) {
+ attributesSet = new HashSet<String>();
+ attributes.put(elementName, attributesSet);
+ }
+
+ return attributesSet;
+ }
+
+ public void serialize(OutputStream os) throws Exception {
+ ObjectOutputStream oos = null;
+ try {
+ oos = new ObjectOutputStream(os);
+ oos.writeObject(attributes);
+ } finally {
+ try {
+ if (oos != null) {
+ oos.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void parse(String schemaSource, String namespace) throws Exception {
+ XSOMParser parser = new XSOMParser();
+ parser.parse(new File(schemaSource));
+ XSSchemaSet sset = parser.getResult();
+ XSSchema cdkXhtmlSchema = sset.getSchema(namespace);
+
+ Iterator<XSElementDecl> elements = cdkXhtmlSchema.iterateElementDecls();
+ while (elements.hasNext()) {
+ XSElementDecl element = elements.next();
+ XSComplexType complexType = element.getType().asComplexType();
+ if (complexType != null) {
+ Set<String> attributesSet = getAttributesSet(element.getName());
+
+ Collection<? extends XSAttributeUse> uses = complexType.getAttributeUses();
+ for (XSAttributeUse xsAttributeUse : uses) {
+ XSAttributeDecl attributeDecl = xsAttributeUse.getDecl();
+ String attributeNamespace = attributeDecl.getTargetNamespace();
+ if (namespace.equals(attributeNamespace) || attributeNamespace.length() == 0) {
+ attributesSet.add(attributeDecl.getName());
+ }
+ }
+ }
+ }
+ }
+}
+
+try {
+ def targetNamespaceParam = project.properties['targetNamespace'];
+
+ def sourceSchemaParam = project.properties['sourceSchema'];
+ String sourceSchemaFileName = project.basedir.getAbsolutePath() + '/' +
sourceSchemaParam;
+
+ def outputFileParam = project.properties['outputFile'];
+ String outputFileName = project.build.outputDirectory + '/' + outputFileParam;
+
+ log.info("Parsing: " + sourceSchemaFileName);
+ log.info("Writing serialized attributes to: " + outputFileName);
+
+ File outputFile = new File(outputFileName);
+
+ outputFile.getParentFile().mkdirs();
+
+ if (outputFile.exists()) {
+ outputFile.delete();
+ }
+ outputFile.createNewFile();
+
+ SchemaParser parser = new SchemaParser();
+ parser.parse(sourceSchemaFileName, targetNamespaceParam);
+ parser.serialize(new FileOutputStream(outputFileName));
+} catch (Exception e) {
+ fail(e);
+}
Show replies by date