You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/05/18 21:50:31 UTC
svn commit: r776065 - in /incubator/pivot/trunk/wtk:
src/pivot/wtkx/JavaTranslator.java src/pivot/wtkx/WTKXCompiler.java
test/pivot/wtkx/test/JavaTranslatorTest.java
test/pivot/wtkx/test/java_translator_test.wtkx
Author: tvolkert
Date: Mon May 18 19:50:31 2009
New Revision: 776065
URL: http://svn.apache.org/viewvc?rev=776065&view=rev
Log:
Moved WTKXCompiler to JavaTranslator, and added simple test case
Added:
incubator/pivot/trunk/wtk/src/pivot/wtkx/JavaTranslator.java
- copied, changed from r775993, incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXCompiler.java
incubator/pivot/trunk/wtk/test/pivot/wtkx/test/JavaTranslatorTest.java
incubator/pivot/trunk/wtk/test/pivot/wtkx/test/java_translator_test.wtkx
Removed:
incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXCompiler.java
Copied: incubator/pivot/trunk/wtk/src/pivot/wtkx/JavaTranslator.java (from r775993, incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXCompiler.java)
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/JavaTranslator.java?p2=incubator/pivot/trunk/wtk/src/pivot/wtkx/JavaTranslator.java&p1=incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXCompiler.java&r1=775993&r2=776065&rev=776065&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXCompiler.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/JavaTranslator.java Mon May 18 19:50:31 2009
@@ -16,7 +16,6 @@
*/
package pivot.wtkx;
-import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -28,17 +27,11 @@
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Method;
-import java.io.InputStream;
import java.net.URI;
-import java.net.URL;
-import java.nio.CharBuffer;
-import javax.lang.model.SourceVersion;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject;
-import javax.tools.OptionChecker;
-import javax.tools.Tool;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
@@ -49,16 +42,14 @@
import pivot.collections.Dictionary;
import pivot.collections.List;
import pivot.collections.Sequence;
-import pivot.serialization.Serializer;
-import pivot.serialization.SerializationException;
import pivot.util.ListenerList;
/**
- * Loads an object hierarchy from an XML document into compilable file objects.
+ * Translates WTKX documents into compilable Java file objects.
*
* @author tvolkert
*/
-public class WTKXCompiler implements Tool, OptionChecker {
+public class JavaTranslator {
/**
* A generated Java file object.
*
@@ -140,6 +131,8 @@
}
/**
+ * A parsed XML element.
+ *
* @author gbrown
*/
private static class Element {
@@ -169,6 +162,8 @@
}
/**
+ * A parsed XML attribute.
+ *
* @author gbrown
*/
private static class Attribute {
@@ -190,27 +185,13 @@
private static final String WTKX_PREFIX = "WTKX";
private static final String JAVA_SUFFIX = ".java";
- private static final String SPACE = " ";
+ private static final String SPACE = "";
- public WTKXCompiler() {
+ public JavaTranslator() {
xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true);
}
- public JavaFileObject readObject(URL location) throws IOException,
- SerializationException {
- if (location == null) {
- throw new IllegalArgumentException("location is null.");
- }
-
- InputStream inputStream = new BufferedInputStream(location.openStream());
- try {
- return readObject(inputStream);
- } finally {
- inputStream.close();
- }
- }
-
/**
* Reads WTKX input stream into a compilable file object. The file object
* will hold a class that implements the {@link Bindable.ObjectHierarchy}
@@ -219,26 +200,43 @@
* @param inputStream
* The data stream from which the WTKX will be read
*
+ * @param className
+ * The fully qualified class name of the class to generate.
+ *
* @return
* The compilable java file object represented by the WTKX
*/
- public JavaFileObject readObject(InputStream inputStream) throws IOException,
- SerializationException {
+ public JavaFileObject translate(InputStream inputStream, String className)
+ throws IOException {
if (inputStream == null) {
throw new IllegalArgumentException("inputStream is null.");
}
+ if (className == null) {
+ throw new IllegalArgumentException("className is null.");
+ }
+
JavaFile javaFile = new JavaFile();
+ String packageName = null;
+
+ // Separate the package name from the un-qualified class name
+ int classDeliminatorIndex = className.lastIndexOf('.');
+ if (classDeliminatorIndex != -1) {
+ if (classDeliminatorIndex == 0) {
+ throw new IllegalArgumentException(className + " is not a valid class name.");
+ }
+
+ packageName = className.substring(0, classDeliminatorIndex);
+ className = className.substring(classDeliminatorIndex + 1);
+ }
Writer writer = javaFile.openWriter();
try {
- String[] path = javaFile.getName().split(File.separator);
- String tmpClassName = path[path.length - 1].split("\\.")[0];
- tmpClassName = "Foo";
+ if (packageName != null) {
+ writer.write("package " + packageName + ";\n\n");
+ }
writer.write(String.format(
- "package pivot.wtkx.test;\n" +
- "\n" +
"import pivot.collections.HashMap;\n" +
"import pivot.wtkx.Bindable;\n" +
"\n" +
@@ -254,7 +252,7 @@
"%1$4s@SuppressWarnings({\"unchecked\", \"cast\"})\n" +
"%1$4spublic <T> T getRootObject() {\n" +
"%1$8sObject result = null;\n",
- SPACE, tmpClassName));
+ SPACE, className));
// Parse the XML stream
Element element = null;
@@ -280,19 +278,19 @@
if (prefix != null
&& prefix.equals(WTKXSerializer.WTKX_PREFIX)) {
if (element == null) {
- throw new SerializationException(prefix + ":" + localName
+ throw new IOException(prefix + ":" + localName
+ " is not a valid root element.");
}
if (localName.equals(WTKXSerializer.INCLUDE_TAG)) {
// TODO
- throw new SerializationException(prefix + ":" + localName
+ throw new IOException(prefix + ":" + localName
+ " compilation is not yet implemented.");
} else if (localName.equals(WTKXSerializer.SCRIPT_TAG)) {
- throw new SerializationException(prefix + ":" + localName
+ throw new IOException(prefix + ":" + localName
+ " tags may not be compiled.");
} else {
- throw new SerializationException(prefix + ":" + localName
+ throw new IOException(prefix + ":" + localName
+ " is not a valid tag.");
}
} else {
@@ -322,30 +320,33 @@
if (Character.isUpperCase(localName.charAt(0))) {
// The element represents a typed object
if (namespaceURI == null) {
- throw new SerializationException("No XML namespace specified for "
+ throw new IOException("No XML namespace specified for "
+ localName + " tag.");
}
- String className = namespaceURI + "." + localName;
- writer.write(String.format
- ("%8s%s o%d = new %s();\n", SPACE, className, ++Element.counter, className));
+ String elementClassName = namespaceURI + "." + localName;
+ writer.write(String.format("%8s%s o%d = new %s();\n",
+ SPACE, elementClassName, ++Element.counter, elementClassName));
try {
- className = namespaceURI + "." + localName.replace('.', '$');
- Class<?> type = Class.forName(className, false, getClass().getClassLoader());
+ elementClassName = namespaceURI + "." + localName.replace('.', '$');
+ Class<?> type = Class.forName(elementClassName,
+ false, getClass().getClassLoader());
element = new Element(element, Element.Type.INSTANCE,
attributes, type, Element.counter);
} catch(Exception exception) {
- throw new SerializationException(exception);
+ throw new IOException(exception);
}
} else {
// The element represents a property
if (element == null) {
- throw new SerializationException("Root node must represent a typed object.");
+ throw new IOException
+ ("Root node must represent a typed object.");
}
if (element.type != Element.Type.INSTANCE) {
- throw new SerializationException("Property elements must apply to typed objects.");
+ throw new IOException
+ ("Property elements must apply to typed objects.");
}
Class<?> type = (Class<?>)element.clazz;
@@ -366,7 +367,7 @@
attributes, valueType, Element.counter);
} else {
if (attributes.getLength() > 0) {
- throw new SerializationException
+ throw new IOException
("Writable property elements cannot have attributes.");
}
@@ -433,7 +434,7 @@
// The element is an untyped object
for (Attribute attribute : element.attributes) {
if (Character.isUpperCase(attribute.localName.charAt(0))) {
- throw new SerializationException
+ throw new IOException
("Static setters are only supported for typed instances.");
}
@@ -458,8 +459,9 @@
+ attribute.localName.substring(0, attribute.localName.length()
- (propertyName.length() + 1));
- writer.write(String.format("%8s%s.%s(o%d, %s);\n", SPACE, propertyClassName,
- setterMethodName, element.ref, resolve(attribute.value, attributeType)));
+ writer.write(String.format("%8s%s.%s(o%d, %s);\n",
+ SPACE, propertyClassName, setterMethodName, element.ref,
+ resolve(attribute.value, attributeType)));
} else {
if (attributeType != null
&& ListenerList.class.isAssignableFrom(attributeType)) {
@@ -520,7 +522,7 @@
reader.close();
} catch(XMLStreamException exception) {
- throw new SerializationException(exception);
+ throw new IOException(exception);
}
// Close method declaration
@@ -536,30 +538,6 @@
return javaFile;
}
- /**
- * {@inheritDoc}
- */
- public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
- // TODO
- return 0;
- }
-
- /**
- * {@inheritDoc}
- */
- public java.util.Set<SourceVersion> getSourceVersions() {
- return java.util.Collections.unmodifiableSet
- (java.util.EnumSet.range(SourceVersion.RELEASE_5, SourceVersion.latest()));
- }
-
- /**
- * {@inheritDoc}
- */
- public int isSupportedOption(String option) {
- // TODO
- return -1;
- }
-
private String resolve(String attributeValue, Class<?> propertyType)
throws IOException {
String result = null;
Added: incubator/pivot/trunk/wtk/test/pivot/wtkx/test/JavaTranslatorTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/test/pivot/wtkx/test/JavaTranslatorTest.java?rev=776065&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/test/pivot/wtkx/test/JavaTranslatorTest.java (added)
+++ incubator/pivot/trunk/wtk/test/pivot/wtkx/test/JavaTranslatorTest.java Mon May 18 19:50:31 2009
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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 pivot.wtkx.test;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.ToolProvider;
+
+import pivot.wtkx.JavaTranslator;
+
+/**
+ * @author tvolkert
+ */
+public class JavaTranslatorTest {
+ public static void main(String[] args) throws Exception {
+ JavaTranslator javaTranslator = new JavaTranslator();
+
+ InputStream inputStream = JavaTranslatorTest.class.getResourceAsStream
+ ("java_translator_test.wtkx");
+
+ JavaFileObject javaFileObject = javaTranslator.translate(inputStream,
+ "pivot.wtkx.test.java_translator_test_WTKX");
+
+ System.out.println(javaFileObject.getCharContent(true));
+
+ ArrayList<JavaFileObject> javaFileObjects = new ArrayList<JavaFileObject>(1);
+ javaFileObjects.add(javaFileObject);
+
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, null, null, javaFileObjects);
+
+ task.call();
+ System.out.println("Class compiled successfully");
+
+ javaFileObject.delete();
+ }
+}
Added: incubator/pivot/trunk/wtk/test/pivot/wtkx/test/java_translator_test.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/test/pivot/wtkx/test/java_translator_test.wtkx?rev=776065&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/test/pivot/wtkx/test/java_translator_test.wtkx (added)
+++ incubator/pivot/trunk/wtk/test/pivot/wtkx/test/java_translator_test.wtkx Mon May 18 19:50:31 2009
@@ -0,0 +1,216 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to you 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.
+-->
+
+<FlowPane xmlns:wtkx="http://incubator.apache.org/pivot/wtkx/1.1"
+ xmlns:content="pivot.wtk.content" xmlns="pivot.wtk">
+ <Border>
+ <content>
+ <FlowPane orientation="vertical" styles="{padding:{top:2, left:4, bottom:4, right:4}, spacing:10}">
+ <FlowPane orientation="vertical">
+ <Label text="Basic Push Buttons" styles="{fontBold:true}"/>
+ <FlowPane>
+ <PushButton buttonData="One"/>
+ <PushButton buttonData="Two"/>
+ <PushButton buttonData="Three" enabled="false"/>
+ </FlowPane>
+ </FlowPane>
+
+ <FlowPane orientation="vertical">
+ <Label text="Ungrouped Toggle Buttons" styles="{fontBold:true}"/>
+ <FlowPane>
+ <PushButton buttonData="One" toggleButton="true"/>
+ <PushButton buttonData="Two" toggleButton="true"/>
+ <PushButton buttonData="Three" toggleButton="true" enabled="false"/>
+ </FlowPane>
+ </FlowPane>
+
+ <FlowPane orientation="vertical">
+ <Label text="Grouped Toggle Buttons" styles="{fontBold:true}"/>
+ <FlowPane>
+ <PushButton buttonData="One" toggleButton="true" group="toggleButtons"/>
+ <PushButton buttonData="Two" toggleButton="true" group="toggleButtons"/>
+ <PushButton buttonData="Three" toggleButton="true" group="toggleButtons" selected="true" enabled="false"/>
+ </FlowPane>
+ </FlowPane>
+
+ <FlowPane orientation="vertical">
+ <Label text="Image Buttons" styles="{fontBold:true}"/>
+ <FlowPane>
+ <PushButton>
+ <buttonData>
+ <content:ButtonData icon="@bell.png" text="Bell"/>
+ </buttonData>
+ </PushButton>
+ <PushButton>
+ <buttonData>
+ <content:ButtonData icon="@clock.png" text="Clock"/>
+ </buttonData>
+ <dataRenderer>
+ <content:ButtonDataRenderer orientation="vertical"/>
+ </dataRenderer>
+ </PushButton>
+ <PushButton enabled="false">
+ <buttonData>
+ <content:ButtonData icon="@house.png" text="House"/>
+ </buttonData>
+ </PushButton>
+ </FlowPane>
+ </FlowPane>
+
+ <FlowPane orientation="vertical">
+ <Label text="Toolbar Buttons" styles="{fontBold:true}"/>
+ <FlowPane>
+ <PushButton styles="{toolbar:true}">
+ <buttonData>
+ <content:ButtonData icon="@bell.png"/>
+ </buttonData>
+ </PushButton>
+ <PushButton styles="{toolbar:true}">
+ <buttonData>
+ <content:ButtonData icon="@clock.png"/>
+ </buttonData>
+ </PushButton>
+ <PushButton enabled="false" styles="{toolbar:true}">
+ <buttonData>
+ <content:ButtonData icon="@house.png"/>
+ </buttonData>
+ </PushButton>
+ </FlowPane>
+ </FlowPane>
+ </FlowPane>
+ </content>
+ </Border>
+
+ <Border>
+ <content>
+ <FlowPane orientation="vertical" styles="{padding:{top:2, left:4, bottom:4, right:4}, spacing:10}">
+ <FlowPane orientation="vertical">
+ <Label text="Basic Radio Buttons" styles="{fontBold:true}"/>
+ <FlowPane>
+ <RadioButton buttonData="One" group="radioButtons"/>
+ <RadioButton buttonData="Two" group="radioButtons"/>
+ <RadioButton buttonData="Three" group="radioButtons" selected="true" enabled="false"/>
+ </FlowPane>
+ </FlowPane>
+
+ <FlowPane orientation="vertical">
+ <Label text="Image Radio Buttons" styles="{fontBold:true}"/>
+ <FlowPane orientation="vertical">
+ <RadioButton group="imageRadioButtons">
+ <buttonData>
+ <content:ButtonData icon="@bell.png" text="Bell"/>
+ </buttonData>
+ </RadioButton>
+ <RadioButton group="imageRadioButtons">
+ <buttonData>
+ <content:ButtonData icon="@clock.png" text="Clock"/>
+ </buttonData>
+ </RadioButton>
+ <RadioButton group="imageRadioButtons" selected="true" enabled="false">
+ <buttonData>
+ <content:ButtonData icon="@house.png" text="House"/>
+ </buttonData>
+ </RadioButton>
+ </FlowPane>
+ </FlowPane>
+ </FlowPane>
+ </content>
+ </Border>
+
+ <Border>
+ <content>
+ <FlowPane orientation="vertical" styles="{padding:{top:2, left:4, bottom:4, right:4}, spacing:10}">
+ <FlowPane orientation="vertical">
+ <Label text="Basic Checkboxes" styles="{fontBold:true}"/>
+ <FlowPane>
+ <Checkbox buttonData="One"/>
+ <Checkbox buttonData="Two"/>
+ <Checkbox buttonData="Three" selected="true" enabled="false"/>
+ </FlowPane>
+ </FlowPane>
+
+ <FlowPane orientation="vertical">
+ <Label text="Image Checkboxes" styles="{fontBold:true}"/>
+ <FlowPane orientation="vertical">
+ <Checkbox>
+ <buttonData>
+ <content:ButtonData icon="@clock.png" text="Clock"/>
+ </buttonData>
+ </Checkbox>
+ <Checkbox>
+ <buttonData>
+ <content:ButtonData icon="@bell.png" text="Bell"/>
+ </buttonData>
+ </Checkbox>
+ <Checkbox selected="true" enabled="false">
+ <buttonData>
+ <content:ButtonData icon="@house.png" text="House"/>
+ </buttonData>
+ </Checkbox>
+ </FlowPane>
+ </FlowPane>
+
+ <FlowPane orientation="vertical">
+ <Label text="Tri-state Checkboxes" styles="{fontBold:true}"/>
+ <FlowPane orientation="vertical">
+ <Checkbox buttonData="Read" triState="true" state="selected"/>
+ <Checkbox buttonData="Write" triState="true" state="unselected"/>
+ <Checkbox buttonData="Execute" triState="true" state="mixed" enabled="false"/>
+ </FlowPane>
+ </FlowPane>
+ </FlowPane>
+ </content>
+ </Border>
+
+ <Border>
+ <content>
+ <FlowPane orientation="vertical" styles="{padding:{top:2, left:4, bottom:4, right:4}, spacing:10}">
+ <FlowPane orientation="vertical">
+ <Label text="Basic Link Buttons" styles="{fontBold:true}"/>
+ <FlowPane>
+ <LinkButton buttonData="One"/>
+ <LinkButton buttonData="Two"/>
+ <LinkButton buttonData="Three" enabled="false"/>
+ </FlowPane>
+ </FlowPane>
+
+ <FlowPane orientation="vertical">
+ <Label text="Image Link Buttons" styles="{fontBold:true}"/>
+ <FlowPane orientation="vertical">
+ <LinkButton>
+ <buttonData>
+ <content:ButtonData icon="@bell.png" text="Bell"/>
+ </buttonData>
+ </LinkButton>
+ <LinkButton>
+ <buttonData>
+ <content:ButtonData icon="@clock.png" text="Clock"/>
+ </buttonData>
+ </LinkButton>
+ <LinkButton enabled="false">
+ <buttonData>
+ <content:ButtonData icon="@house.png" text="House"/>
+ </buttonData>
+ </LinkButton>
+ </FlowPane>
+ </FlowPane>
+ </FlowPane>
+ </content>
+ </Border>
+</FlowPane>
+