OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3577 ae02f08e...
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Fri, 11 Sep 2009 01:14:31 +0000 (01:14 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Fri, 11 Sep 2009 01:14:31 +0000 (01:14 +0000)
.classpath
.project
.settings/org.eclipse.jdt.core.prefs
src/main/java/org/xerial/lens/ObjectLens.java
src/main/java/org/xerial/lens/ObjectMapper.java
src/main/java/org/xerial/lens/relation/query/impl/LensQueryLexer.java
src/main/java/org/xerial/lens/relation/query/impl/LensQueryParser.java
src/main/java/org/xerial/util/bean/TypeInfo.java

index 74fee5c..d2ce440 100644 (file)
@@ -1,14 +1,14 @@
-<classpath>
-  <classpathentry kind="src" path="src/main/java"/>
-  <classpathentry kind="src" path="src/main/resources" excluding="**/.svn/**|**/*.java"/>
-  <classpathentry kind="src" path="src/test/java" output="target/test-classes"/>
-  <classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>
-  <classpathentry kind="output" path="target/classes"/>
-  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-  <classpathentry kind="var" path="M2_REPO/antlr/antlr/2.7.7/antlr-2.7.7.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/antlr/antlr/3.1.3/antlr-3.1.3.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/antlr/antlr-runtime/3.1.3/antlr-runtime-3.1.3.jar"/>
-  <classpathentry kind="var" path="M2_REPO/junit/junit/4.1/junit-4.1.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/antlr/stringtemplate/3.2/stringtemplate-3.2.jar"/>
-  <classpathentry kind="var" path="M2_REPO/xpp3/xpp3/1.1.4c/xpp3-1.1.4c.jar"/>
+<classpath>\r
+  <classpathentry kind="src" path="src/main/java"/>\r
+  <classpathentry kind="src" path="src/main/resources" excluding="**/.svn/**|**/*.java"/>\r
+  <classpathentry kind="src" path="src/test/java" output="target/test-classes"/>\r
+  <classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>\r
+  <classpathentry kind="output" path="target/classes"/>\r
+  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>\r
+  <classpathentry kind="var" path="M2_REPO/antlr/antlr/2.7.7/antlr-2.7.7.jar"/>\r
+  <classpathentry kind="var" path="M2_REPO/org/antlr/antlr/3.1.3/antlr-3.1.3.jar"/>\r
+  <classpathentry kind="var" path="M2_REPO/org/antlr/antlr-runtime/3.1.3/antlr-runtime-3.1.3.jar"/>\r
+  <classpathentry kind="var" path="M2_REPO/junit/junit/4.1/junit-4.1.jar"/>\r
+  <classpathentry kind="var" path="M2_REPO/org/antlr/stringtemplate/3.2/stringtemplate-3.2.jar"/>\r
+  <classpathentry kind="var" path="M2_REPO/xpp3/xpp3/1.1.4c/xpp3-1.1.4c.jar"/>\r
 </classpath>
\ No newline at end of file
index e203b62..c7e08ce 100644 (file)
--- a/.project
+++ b/.project
@@ -1,23 +1,23 @@
-<projectDescription>
-  <name>xerial-core</name>
-  <comment>xerial-core is a common utilities for the Xerial project.</comment>
-  <projects/>
-  <buildSpec>
-    <buildCommand>
-      <name>org.eclipse.hyades.probekit.probekitbuilder</name>
-    </buildCommand>
-    <buildCommand>
-      <name>org.eclipse.jdt.core.javabuilder</name>
-    </buildCommand>
-    <buildCommand>
-      <name>org.eclipse.hyades.probekit.probekitbundler</name>
-    </buildCommand>
-    <buildCommand>
-      <name>org.eclipse.hyades.test.tools.core.java.junit.builder</name>
-    </buildCommand>
-  </buildSpec>
-  <natures>
-    <nature>org.eclipse.jdt.core.javanature</nature>
-    <nature>org.eclipse.hyades.probekit.probekitnature</nature>
-  </natures>
+<projectDescription>\r
+  <name>xerial-core</name>\r
+  <comment>xerial-core is a common utilities for the Xerial project.</comment>\r
+  <projects/>\r
+  <buildSpec>\r
+    <buildCommand>\r
+      <name>org.eclipse.hyades.probekit.probekitbuilder</name>\r
+    </buildCommand>\r
+    <buildCommand>\r
+      <name>org.eclipse.jdt.core.javabuilder</name>\r
+    </buildCommand>\r
+    <buildCommand>\r
+      <name>org.eclipse.hyades.probekit.probekitbundler</name>\r
+    </buildCommand>\r
+    <buildCommand>\r
+      <name>org.eclipse.hyades.test.tools.core.java.junit.builder</name>\r
+    </buildCommand>\r
+  </buildSpec>\r
+  <natures>\r
+    <nature>org.eclipse.jdt.core.javanature</nature>\r
+    <nature>org.eclipse.hyades.probekit.probekitnature</nature>\r
+  </natures>\r
 </projectDescription>
\ No newline at end of file
index c8b276f..ba491a6 100644 (file)
@@ -1,4 +1,4 @@
-#Thu Sep 10 14:33:14 JST 2009\r
+#Thu Sep 10 17:47:23 JST 2009\r
 eclipse.preferences.version=1\r
 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled\r
 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5\r
@@ -10,9 +10,9 @@ org.eclipse.jdt.core.compiler.debug.sourceFile=generate
 org.eclipse.jdt.core.compiler.problem.assertIdentifier=error\r
 org.eclipse.jdt.core.compiler.problem.enumIdentifier=error\r
 org.eclipse.jdt.core.compiler.source=1.5\r
-org.eclipse.jdt.core.formatter.align_type_members_on_columns=true\r
+org.eclipse.jdt.core.formatter.align_type_members_on_columns=false\r
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16\r
-org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=82\r
+org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16\r
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=16\r
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16\r
 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=16\r
@@ -20,14 +20,14 @@ org.eclipse.jdt.core.formatter.alignment_for_assignment=0
 org.eclipse.jdt.core.formatter.alignment_for_binary_expression=16\r
 org.eclipse.jdt.core.formatter.alignment_for_compact_if=16\r
 org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=80\r
-org.eclipse.jdt.core.formatter.alignment_for_enum_constants=82\r
+org.eclipse.jdt.core.formatter.alignment_for_enum_constants=0\r
 org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=16\r
 org.eclipse.jdt.core.formatter.alignment_for_multiple_fields=16\r
 org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration=16\r
 org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration=16\r
 org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=16\r
 org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration=16\r
-org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=82\r
+org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=16\r
 org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration=16\r
 org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration=16\r
 org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration=16\r
@@ -52,7 +52,7 @@ org.eclipse.jdt.core.formatter.brace_position_for_enum_constant=end_of_line
 org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration=end_of_line\r
 org.eclipse.jdt.core.formatter.brace_position_for_method_declaration=end_of_line\r
 org.eclipse.jdt.core.formatter.brace_position_for_switch=end_of_line\r
-org.eclipse.jdt.core.formatter.brace_position_for_type_declaration=next_line\r
+org.eclipse.jdt.core.formatter.brace_position_for_type_declaration=end_of_line\r
 org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment=false\r
 org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment=false\r
 org.eclipse.jdt.core.formatter.comment.format_block_comments=false\r
@@ -257,7 +257,7 @@ org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false
 org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=true\r
 org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false\r
 org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false\r
-org.eclipse.jdt.core.formatter.lineSplit=120\r
+org.eclipse.jdt.core.formatter.lineSplit=100\r
 org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column=false\r
 org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=false\r
 org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=0\r
index 580db63..7a0901d 100644 (file)
@@ -57,11 +57,11 @@ import org.xerial.util.reflect.ReflectionUtil;
  * @author leo\r
  * \r
  */\r
-public class ObjectLens\r
-{\r
-    private static Logger                          _logger = Logger.getLogger(ObjectLens.class);\r
+public class ObjectLens {\r
 \r
-    private static HashMap<Class< ? >, ObjectLens> cache   = new HashMap<Class< ? >, ObjectLens>();\r
+    private static Logger _logger = Logger.getLogger(ObjectLens.class);\r
+\r
+    private static HashMap<Class< ? >, ObjectLens> cache = new HashMap<Class< ? >, ObjectLens>();\r
 \r
     /**\r
      * Get the lens of the target type\r
@@ -78,14 +78,14 @@ public class ObjectLens
         }\r
     }\r
 \r
-    private Class< ? >            targetType;\r
+    private Class< ? > targetType;\r
 \r
-    private List<ParameterGetter> getterContainer         = new ArrayList<ParameterGetter>();\r
-    private List<ParameterSetter> setterContainer         = new ArrayList<ParameterSetter>();\r
-    private List<RelationSetter>  relationSetterContainer = new ArrayList<RelationSetter>();\r
-    private ParameterSetter       valueSetter             = null;\r
+    private List<ParameterGetter> getterContainer = new ArrayList<ParameterGetter>();\r
+    private List<ParameterSetter> setterContainer = new ArrayList<ParameterSetter>();\r
+    private List<RelationSetter> relationSetterContainer = new ArrayList<RelationSetter>();\r
+    private ParameterSetter valueSetter = null;\r
 \r
-    private RelationSetter        propertySetter          = null;\r
+    private RelationSetter propertySetter = null;\r
 \r
     public void setProperty(Object target, Object key, Object value) throws XerialException {\r
         if (propertySetter == null)\r
@@ -151,7 +151,8 @@ public class ObjectLens
                 }\r
                 else if (TypeInfo.isMap(fieldType)) {\r
                     // TODO map putter\r
-                    Pair<Type, Type> keyValueTypes = ReflectionUtil.getGenericMapElementType(eachField);\r
+                    Pair<Type, Type> keyValueTypes = ReflectionUtil\r
+                            .getGenericMapElementType(eachField);\r
                     Pair<String, String> keyValueNames = pickRelationName(eachField.getName());\r
                     if (keyValueNames == null) {\r
                         // infer key and value names from type parameters in Map<Key, Value>\r
@@ -162,12 +163,14 @@ public class ObjectLens
                 else if (TypeInfo.isCollection(fieldType)) {\r
                     Class< ? > elementType = ReflectionUtil.getRawClass(ReflectionUtil\r
                             .getGenericCollectionElementType(eachField));\r
-                    setterContainer.add(ParameterSetter.newSetter(elementType, paramName, eachField));\r
+                    setterContainer.add(ParameterSetter\r
+                            .newSetter(elementType, paramName, eachField));\r
                     getterContainer.add(ParameterGetter.newFieldGetter(eachField, paramName));\r
                 }\r
                 else {\r
                     if (!paramName.equals("value"))\r
-                        setterContainer.add(ParameterSetter.newSetter(fieldType, paramName, eachField));\r
+                        setterContainer.add(ParameterSetter.newSetter(fieldType, paramName,\r
+                                eachField));\r
                     else\r
                         valueSetter = ParameterSetter.newSetter(fieldType, paramName, eachField);\r
                     getterContainer.add(ParameterGetter.newFieldGetter(eachField, paramName));\r
@@ -187,13 +190,16 @@ public class ObjectLens
                 case 1: {\r
                     String paramName = pickPropertyName(methodName);\r
                     Class< ? > parentOfTheSetter = eachMethod.getDeclaringClass();\r
-                    if ((TypeInfo.isCollection(parentOfTheSetter) || TypeInfo.isMap(parentOfTheSetter))\r
+                    if ((TypeInfo.isCollection(parentOfTheSetter) || TypeInfo\r
+                            .isMap(parentOfTheSetter))\r
                             && paramName.equals("all"))\r
                         break;\r
 \r
                     if (paramName.length() <= 0 && TypeInfo.isCollection(parentOfTheSetter)) {\r
-                        Class< ? > elementType = BeanUtil.resolveActualTypeOfCollectionElement(targetType, argTypes[0]);\r
-                        setterContainer.add(ParameterSetter.newSetter(elementType, "entry", eachMethod));\r
+                        Class< ? > elementType = BeanUtil.resolveActualTypeOfCollectionElement(\r
+                                targetType, argTypes[0]);\r
+                        setterContainer.add(ParameterSetter.newSetter(elementType, "entry",\r
+                                eachMethod));\r
                     }\r
                     else\r
                         addNewSetter(setterContainer, paramName, eachMethod);\r
@@ -206,17 +212,18 @@ public class ObjectLens
 \r
                     // relation adder\r
 \r
-                    Pair<String, String> relName = pickRelationName(pickPropertyName(methodName, false));\r
+                    Pair<String, String> relName = pickRelationName(pickPropertyName(methodName,\r
+                            false));\r
                     if (relName == null) {\r
                         // infer relation node names\r
                         if (TypeInfo.isMap(eachMethod.getDeclaringClass())) {\r
 \r
-                            Class< ? >[] mapElementType = BeanUtil.resolveActualTypeOfMapElement(targetType, eachMethod\r
-                                    .getParameterTypes());\r
+                            Class< ? >[] mapElementType = BeanUtil.resolveActualTypeOfMapElement(\r
+                                    targetType, eachMethod.getParameterTypes());\r
 \r
                             // map.put(Key, Value)\r
-                            setterContainer\r
-                                    .add(ParameterSetter.newMapEntrySetter(mapElementType[0], mapElementType[1]));\r
+                            setterContainer.add(ParameterSetter.newMapEntrySetter(\r
+                                    mapElementType[0], mapElementType[1]));\r
 \r
                             // (entry, key)\r
                             setterContainer.add(ParameterSetter.newKeySetter(mapElementType[0]));\r
@@ -225,13 +232,14 @@ public class ObjectLens
                             continue;\r
                         }\r
                         else {\r
-                            relName = new Pair<String, String>(getCanonicalParameterName(argTypes[0].getSimpleName()),\r
+                            relName = new Pair<String, String>(\r
+                                    getCanonicalParameterName(argTypes[0].getSimpleName()),\r
                                     getCanonicalParameterName(argTypes[1].getSimpleName()));\r
                         }\r
                     }\r
 \r
-                    relationSetterContainer.add(RelationSetter.newRelationSetter(relName.getFirst(), relName\r
-                            .getSecond(), eachMethod));\r
+                    relationSetterContainer.add(RelationSetter.newRelationSetter(\r
+                            relName.getFirst(), relName.getSecond(), eachMethod));\r
                     break;\r
                 }\r
                 }\r
@@ -253,11 +261,12 @@ public class ObjectLens
                     // infer relation node names\r
                     if (TypeInfo.isMap(eachMethod.getDeclaringClass())) {\r
 \r
-                        Class< ? >[] mapElementType = BeanUtil.resolveActualTypeOfMapElement(targetType, eachMethod\r
-                                .getParameterTypes());\r
+                        Class< ? >[] mapElementType = BeanUtil.resolveActualTypeOfMapElement(\r
+                                targetType, eachMethod.getParameterTypes());\r
 \r
                         // map.put(Key, Value)\r
-                        setterContainer.add(ParameterSetter.newMapEntrySetter(mapElementType[0], mapElementType[1]));\r
+                        setterContainer.add(ParameterSetter.newMapEntrySetter(mapElementType[0],\r
+                                mapElementType[1]));\r
 \r
                         // (entry, key)\r
                         setterContainer.add(ParameterSetter.newKeySetter(mapElementType[0]));\r
@@ -266,13 +275,14 @@ public class ObjectLens
                         continue;\r
                     }\r
                     else {\r
-                        propertySetter = RelationSetter.newRelationSetter("key", "value", eachMethod);\r
+                        propertySetter = RelationSetter.newRelationSetter("key", "value",\r
+                                eachMethod);\r
                         continue;\r
                     }\r
                 }\r
 \r
-                relationSetterContainer.add(RelationSetter.newRelationSetter(relName.getFirst(), relName.getSecond(),\r
-                        eachMethod));\r
+                relationSetterContainer.add(RelationSetter.newRelationSetter(relName.getFirst(),\r
+                        relName.getSecond(), eachMethod));\r
 \r
                 continue;\r
             }\r
@@ -296,7 +306,8 @@ public class ObjectLens
         }\r
     }\r
 \r
-    private static void addNewSetter(List<ParameterSetter> setterContainer, String paramPart, Method m) {\r
+    private static void addNewSetter(List<ParameterSetter> setterContainer, String paramPart,\r
+            Method m) {\r
         Class< ? >[] argTypes = m.getParameterTypes();\r
         if (argTypes.length != 1)\r
             return;\r
@@ -312,8 +323,9 @@ public class ObjectLens
         return;\r
     }\r
 \r
-    static private Pattern propertyNamePattern = Pattern.compile("^(set|get|add|put|append)((\\S)(\\S*))?");\r
-    static private Pattern pairedNamePattern   = Pattern.compile("([A-Za-z0-9]*)_([A-Za-z0-9]*)");\r
+    static private Pattern propertyNamePattern = Pattern\r
+            .compile("^(set|get|add|put|append)((\\S)(\\S*))?");\r
+    static private Pattern pairedNamePattern = Pattern.compile("([A-Za-z0-9]*)_([A-Za-z0-9]*)");\r
 \r
     static String pickPropertyName(String methodName, boolean canonicalize) {\r
         Matcher m = null;\r
index 5de09eb..b5abaf5 100644 (file)
@@ -61,16 +61,16 @@ import org.xerial.util.tree.TreeParser;
 public class ObjectMapper {
     private static Logger _logger = Logger.getLogger(ObjectMapper.class);
 
+    //-------------------------------------
+    // dynamic parameters required for mapping tree-structured data to an object
     // id -> corresponding object instance
+    //-------------------------------------
     private HashMap<Long, Object> objectHolder = new HashMap<Long, Object>();
-
-    // schema -> binder 
-    private HashMap<Schema, Binder> schema2binder = new HashMap<Schema, Binder>();
-
     private Deque<Object> contextNodeStack = new ArrayDeque<Object>();
-    private final QuerySet qs;
 
-    private static HashMap<Class< ? >, ObjectMapper> prebuiltMapper = new HashMap<Class< ? >, ObjectMapper>();
+    private static QuerySet qs = null;
+    // schema -> binder 
+    private static HashMap<Schema, Binder> schema2binder = new HashMap<Schema, Binder>();
 
     /**
      * interface for invoking setters or field setters of the object
@@ -171,81 +171,14 @@ public class ObjectMapper {
         }
     }
 
-    public <T> ObjectMapper(Class<T> targetType) throws XerialException {
-        qs = buildQuery(targetType);
-    }
-
-    public static ObjectMapper getMapper(Class< ? > targetType) throws XerialException {
-        if (prebuiltMapper.containsKey(targetType))
-            return prebuiltMapper.get(targetType);
-        else {
-            ObjectMapper newInstance = new ObjectMapper(targetType);
-            prebuiltMapper.put(targetType, newInstance);
-            return newInstance;
-        }
-    }
-
-    public <T> T map(Class<T> targetType, TreeParser parser) throws XerialException {
-
-        T object = TypeInfo.createInstance(targetType);
-        return map(object, parser);
-    }
-
-    public <T> T map(T object, TreeParser parser) throws XerialException {
-        try {
-            if (object == null)
-                throw new XerialError(XerialErrorCode.INVALID_INPUT, "null object");
-
-            if (_logger.isTraceEnabled())
-                _logger.trace("query set: " + qs);
-
-            // set the root object
-            objectHolder.put(0L, object);
-            contextNodeStack.addLast(object);
-
-            AmoebaJoinHandler mapper = new RelationExtracter();
-
-            StreamAmoebaJoin aj = new StreamAmoebaJoin(qs, mapper);
-            aj.sweep(parser);
-            return object;
-        }
-        catch (IOException e) {
-            throw new XerialException(XerialErrorCode.IO_EXCEPTION, e);
-        }
-        catch (Exception e) {
-            throw new XerialException(XerialErrorCode.INHERITED, e);
-        }
-
-    }
-
-    public <T> void find(Class<T> targetType, TreeParser parser,
-            String coreNodeNameOfTheTargetType, ObjectHandler<T> handler) throws XerialException {
-
-        try {
-            AmoebaJoinHandler mapper = new RelationExtracter();
-
-            _logger.info(qs);
-
-            StreamAmoebaJoin aj = new StreamAmoebaJoin(qs, mapper);
-            aj.sweep(parser);
-        }
-        catch (IOException e) {
-            throw new XerialException(XerialErrorCode.IO_EXCEPTION, e);
-        }
-        catch (Exception e) {
-            throw new XerialException(XerialErrorCode.INHERITED, e);
-        }
-
-    }
-
     private QuerySet buildQuery(Class< ? > targetType) {
         QueryBuilder qb = new QueryBuilder();
         qb.build(targetType, "root");
-        return qb.qs.build();
+        return qb.qsBuilder.build();
     }
 
     private class QueryBuilder {
-        QuerySetBuilder qs = new QuerySetBuilder();
+        QuerySetBuilder qsBuilder = new QuerySetBuilder();
         private HashMap<String, Set<Class< ? >>> processedClassTable = new HashMap<String, Set<Class< ? >>>();
 
         //Set<Class< ? >> processedClasses = new HashSet<Class< ? >>();
@@ -286,7 +219,7 @@ public class ObjectMapper {
                     builder.add("entry");
                     builder.add(each.getParameterName());
                     Schema s = builder.build();
-                    qs.addQueryTarget(s);
+                    qsBuilder.addQueryTarget(s);
                     schema2binder.put(s, new AttributeBinder(MapEntry.class, each));
                     continue;
                 }
@@ -298,7 +231,7 @@ public class ObjectMapper {
                 builder.add(each.getParameterName());
 
                 Schema s = builder.build();
-                qs.addQueryTarget(s);
+                qsBuilder.addQueryTarget(s);
 
                 schema2binder.put(s, new AttributeBinder(lens.getTargetType(), each));
             }
@@ -309,7 +242,7 @@ public class ObjectMapper {
 
                 Schema s = new SchemaBuilder().add(each.getCoreNodeName()).add(
                         each.getAttributeNodeName()).build();
-                qs.addQueryTarget(s);
+                qsBuilder.addQueryTarget(s);
 
                 schema2binder.put(s, new RelationBinder(lens, each));
 
@@ -319,6 +252,70 @@ public class ObjectMapper {
 
     }
 
+    public <T> ObjectMapper(Class<T> targetType) throws XerialException {
+        if (qs == null) {
+            schema2binder.clear();
+            qs = buildQuery(targetType);
+        }
+    }
+
+    public static ObjectMapper getMapper(Class< ? > targetType) throws XerialException {
+        return new ObjectMapper(targetType);
+    }
+
+    public <T> T map(Class<T> targetType, TreeParser parser) throws XerialException {
+
+        T object = TypeInfo.createInstance(targetType);
+        return map(object, parser);
+    }
+
+    public <T> T map(T object, TreeParser parser) throws XerialException {
+        try {
+            if (object == null)
+                throw new XerialError(XerialErrorCode.INVALID_INPUT, "null object");
+
+            if (_logger.isTraceEnabled())
+                _logger.trace("query set: " + qs);
+
+            // set the root object
+            objectHolder.put(0L, object);
+            contextNodeStack.addLast(object);
+
+            AmoebaJoinHandler mapper = new RelationExtracter();
+
+            StreamAmoebaJoin aj = new StreamAmoebaJoin(qs, mapper);
+            aj.sweep(parser);
+            return object;
+        }
+        catch (IOException e) {
+            throw new XerialException(XerialErrorCode.IO_EXCEPTION, e);
+        }
+        catch (Exception e) {
+            throw new XerialException(XerialErrorCode.INHERITED, e);
+        }
+
+    }
+
+    public <T> void find(Class<T> targetType, TreeParser parser,
+            String coreNodeNameOfTheTargetType, ObjectHandler<T> handler) throws XerialException {
+
+        try {
+            AmoebaJoinHandler mapper = new RelationExtracter();
+
+            _logger.info(qs);
+
+            StreamAmoebaJoin aj = new StreamAmoebaJoin(qs, mapper);
+            aj.sweep(parser);
+        }
+        catch (IOException e) {
+            throw new XerialException(XerialErrorCode.IO_EXCEPTION, e);
+        }
+        catch (Exception e) {
+            throw new XerialException(XerialErrorCode.INHERITED, e);
+        }
+
+    }
+
     private Object getNodeInstance(Node node, Class< ? > nodeType) throws XerialException {
         Object instance = objectHolder.get(node.nodeID);
         if (instance != null)
index 31c0483..1f06dcb 100644 (file)
@@ -1,4 +1,4 @@
-// $ANTLR 3.1.3 Mar 17, 2009 19:23:44 LensQuery.g 2009-08-06 15:20:41\r
+// $ANTLR 3.1.3 Mar 17, 2009 19:23:44 LensQuery.g 2009-09-10 16:56:16\r
 \r
 /*--------------------------------------------------------------------------\r
  *  Copyright 2009 Taro L. Saito\r
index 3f087dc..ed2f0d9 100644 (file)
@@ -1,4 +1,4 @@
-// $ANTLR 3.1.3 Mar 17, 2009 19:23:44 LensQuery.g 2009-08-06 15:20:40\r
+// $ANTLR 3.1.3 Mar 17, 2009 19:23:44 LensQuery.g 2009-09-10 16:56:16\r
 \r
 /*--------------------------------------------------------------------------\r
  *  Copyright 2009 Taro L. Saito\r
@@ -791,7 +791,7 @@ public class LensQueryParser extends Parser {
 \r
 \r
                     // AST REWRITE\r
-                    // elements: alias, nodeValue, nodeName\r
+                    // elements: nodeName, nodeValue, alias\r
                     // token labels: \r
                     // rule labels: retval\r
                     // token list labels: \r
index d4c8687..00f7f2e 100644 (file)
@@ -59,13 +59,13 @@ import org.xerial.util.Triplet;
  * @author leo
  * 
  */
-public class TypeInfo
-{
-    static private Class< ? >[]        _parameterClass = { int.class, double.class, float.class, long.class,
-            boolean.class, char.class, short.class, String.class, Integer.class, Double.class, Float.class, Long.class,
-            Boolean.class, Character.class, Short.class, Date.class, File.class };
+public class TypeInfo {
+    static private Class< ? >[] _parameterClass = { int.class, double.class, float.class,
+            long.class, boolean.class, char.class, short.class, String.class, Integer.class,
+            Double.class, Float.class, Long.class, Boolean.class, Character.class, Short.class,
+            Date.class, File.class };
 
-    static private HashSet<Class< ? >> basicTypeSet    = new HashSet<Class< ? >>();
+    static private HashSet<Class< ? >> basicTypeSet = new HashSet<Class< ? >>();
     static {
         for (Class< ? > c : _parameterClass)
             basicTypeSet.add(c);
@@ -79,8 +79,8 @@ public class TypeInfo
     /**
      * @param c
      * @return true : if a given class is a single class or array of int,
-     *         double, float, boolean, String, Integer, Double, Float or
-     *         Boolean. otherwise false.
+     *         double, float, boolean, String, Integer, Double, Float, Boolean,
+     *         or File. otherwise false.
      */
     public static boolean isBasicType(Class< ? > c) {
         if (c.isArray())
@@ -209,8 +209,8 @@ public class TypeInfo
 
     public static <T> Object createPrimitiveTypeInstance(Class<T> c) throws XerialException {
         if (!c.isPrimitive())
-            throw new XerialException(XerialErrorCode.InvalidType, String.format("%s is not a primitive", c
-                    .getSimpleName()));
+            throw new XerialException(XerialErrorCode.InvalidType, String.format(
+                    "%s is not a primitive", c.getSimpleName()));
 
         // Boolean.TYPE, Character.TYPE, Byte.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE, Void.TYPE
         if (c == int.class)
@@ -319,8 +319,8 @@ public class TypeInfo
                     }
                 }
 
-                throw new XerialException(XerialErrorCode.NoPublicConstructor, "No public constructor for the class: "
-                        + c.getName() + " is available");
+                throw new XerialException(XerialErrorCode.NoPublicConstructor,
+                        "No public constructor for the class: " + c.getName() + " is available");
 
             }
         }
@@ -332,7 +332,8 @@ public class TypeInfo
         }
     }
 
-    public static Class< ? > getElementTypeOfCollection(Class< ? extends Collection< ? >> collectionType) {
+    public static Class< ? > getElementTypeOfCollection(
+            Class< ? extends Collection< ? >> collectionType) {
         //        collectionType.get
         //        
         //        ParameterizedType pt = getParentParameterizedType(type, Collection.class);