Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2008-04-18 14:53:42 -0400
committerpelder2008-04-18 14:53:42 -0400
commitd1e0bdb37841a369bc71bc4fa868d33f48167e7f (patch)
tree07ea9a9bd3a84c35db476850dba0d6a8100ed453
parent595d9bfab79c8c62de57c49e56dca66de7795d6d (diff)
downloadorg.eclipse.jet-d1e0bdb37841a369bc71bc4fa868d33f48167e7f.tar.gz
org.eclipse.jet-d1e0bdb37841a369bc71bc4fa868d33f48167e7f.tar.xz
org.eclipse.jet-d1e0bdb37841a369bc71bc4fa868d33f48167e7f.zip
[227819] Contribute example of how to load/traverse JDT ASTs with JETs XPath engine
-rw-r--r--examples/org.eclipse.jet.examples.jdt/.classpath7
-rw-r--r--examples/org.eclipse.jet.examples.jdt/.project28
-rw-r--r--examples/org.eclipse.jet.examples.jdt/.settings/org.eclipse.jdt.ui.prefs3
-rw-r--r--examples/org.eclipse.jet.examples.jdt/META-INF/MANIFEST.MF13
-rw-r--r--examples/org.eclipse.jet.examples.jdt/build.properties5
-rw-r--r--examples/org.eclipse.jet.examples.jdt/plugin.xml28
-rw-r--r--examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/Activator.java63
-rw-r--r--examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeAttribute.java77
-rw-r--r--examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeDocumentRoot.java117
-rw-r--r--examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeType.java103
-rw-r--r--examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNode.java330
-rw-r--r--examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeAttribute.java89
-rw-r--r--examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeDocumentRoot.java86
-rw-r--r--examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeType.java85
-rw-r--r--examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/loaders/JavaLoader.java87
15 files changed, 1121 insertions, 0 deletions
diff --git a/examples/org.eclipse.jet.examples.jdt/.classpath b/examples/org.eclipse.jet.examples.jdt/.classpath
new file mode 100644
index 0000000..751c8f2
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/examples/org.eclipse.jet.examples.jdt/.project b/examples/org.eclipse.jet.examples.jdt/.project
new file mode 100644
index 0000000..ef1f89d
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.jet.examples.jdt</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/examples/org.eclipse.jet.examples.jdt/.settings/org.eclipse.jdt.ui.prefs b/examples/org.eclipse.jet.examples.jdt/.settings/org.eclipse.jdt.ui.prefs
new file mode 100644
index 0000000..16715af
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/.settings/org.eclipse.jdt.ui.prefs
@@ -0,0 +1,3 @@
+#Thu Oct 04 12:19:28 EDT 2007
+eclipse.preferences.version=1
+org.eclipse.jdt.ui.text.custom_code_templates=<?xml version\="1.0" encoding\="UTF-8"?><templates><template autoinsert\="true" context\="gettercomment_context" deleted\="false" description\="Comment for getter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.gettercomment" name\="gettercomment">/**\r\n * @return the ${bare_field_name}\r\n */</template><template autoinsert\="true" context\="settercomment_context" deleted\="false" description\="Comment for setter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.settercomment" name\="settercomment">/**\r\n * @param ${param} the ${bare_field_name} to set\r\n */</template><template autoinsert\="true" context\="constructorcomment_context" deleted\="false" description\="Comment for created constructors" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name\="constructorcomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="false" context\="filecomment_context" deleted\="false" description\="Comment for created Java files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.filecomment" name\="filecomment">/**\r\n * &lt;copyright&gt;\r\n *\r\n * Copyright (c) ${year} IBM Corporation and others.\r\n * All rights reserved. This program and the accompanying materials\r\n * are made available under the terms of the Eclipse Public License v1.0\r\n * which accompanies this distribution, and is available at\r\n * http\://www.eclipse.org/legal/epl-v10.html\r\n * \r\n * Contributors\: \r\n * IBM - Initial API and implementation\r\n *\r\n */</template><template autoinsert\="true" context\="typecomment_context" deleted\="false" description\="Comment for created types" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.typecomment" name\="typecomment">/**\r\n * @author ${user}\r\n *\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="fieldcomment_context" deleted\="false" description\="Comment for fields" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name\="fieldcomment">/**\r\n * \r\n */</template><template autoinsert\="true" context\="methodcomment_context" deleted\="false" description\="Comment for non-overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodcomment" name\="methodcomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="overridecomment_context" deleted\="false" description\="Comment for overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.overridecomment" name\="overridecomment">/* (non-Javadoc)\r\n * ${see_to_overridden}\r\n */</template><template autoinsert\="true" context\="delegatecomment_context" deleted\="false" description\="Comment for delegate methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.delegatecomment" name\="delegatecomment">/**\r\n * ${tags}\r\n * ${see_to_target}\r\n */</template><template autoinsert\="true" context\="newtype_context" deleted\="false" description\="Newly created files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.newtype" name\="newtype">${filecomment}\r\n${package_declaration}\r\n\r\n${typecomment}\r\n${type_declaration}</template><template autoinsert\="true" context\="classbody_context" deleted\="false" description\="Code in new class type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.classbody" name\="classbody">\r\n</template><template autoinsert\="true" context\="interfacebody_context" deleted\="false" description\="Code in new interface type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.interfacebody" name\="interfacebody">\r\n</template><template autoinsert\="true" context\="enumbody_context" deleted\="false" description\="Code in new enum type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.enumbody" name\="enumbody">\r\n</template><template autoinsert\="true" context\="annotationbody_context" deleted\="false" description\="Code in new annotation type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.annotationbody" name\="annotationbody">\r\n</template><template autoinsert\="true" context\="catchblock_context" deleted\="false" description\="Code in new catch blocks" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.catchblock" name\="catchblock">// ${todo} Auto-generated catch block\r\n${exception_var}.printStackTrace();</template><template autoinsert\="true" context\="methodbody_context" deleted\="false" description\="Code in created method stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodbody" name\="methodbody">// ${todo} Auto-generated method stub\r\n${body_statement}</template><template autoinsert\="true" context\="constructorbody_context" deleted\="false" description\="Code in created constructor stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorbody" name\="constructorbody">${body_statement}\r\n// ${todo} Auto-generated constructor stub</template><template autoinsert\="true" context\="getterbody_context" deleted\="false" description\="Code in created getters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.getterbody" name\="getterbody">return ${field};</template><template autoinsert\="true" context\="setterbody_context" deleted\="false" description\="Code in created setters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.setterbody" name\="setterbody">${field} \= ${param};</template></templates>
diff --git a/examples/org.eclipse.jet.examples.jdt/META-INF/MANIFEST.MF b/examples/org.eclipse.jet.examples.jdt/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..d970565
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/META-INF/MANIFEST.MF
@@ -0,0 +1,13 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: JET loader for JDT DOM (Java) classes
+Bundle-SymbolicName: org.eclipse.jet.examples.jdt;singleton:=true
+Bundle-Version: 1.0.0
+Bundle-Activator: org.eclipse.jet.examples.jdt.Activator
+Bundle-Vendor: Eclipse.org
+Bundle-Localization: plugin
+Require-Bundle: org.eclipse.core.runtime,
+ org.eclipse.jet,
+ org.eclipse.jdt.core,
+ org.eclipse.core.resources
+Eclipse-LazyStart: true
diff --git a/examples/org.eclipse.jet.examples.jdt/build.properties b/examples/org.eclipse.jet.examples.jdt/build.properties
new file mode 100644
index 0000000..e9863e2
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/build.properties
@@ -0,0 +1,5 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .,\
+ plugin.xml
diff --git a/examples/org.eclipse.jet.examples.jdt/plugin.xml b/examples/org.eclipse.jet.examples.jdt/plugin.xml
new file mode 100644
index 0000000..10736c8
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/plugin.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<?eclipse version="3.2"?>
+<plugin>
+ <extension
+ point="org.eclipse.jet.modelLoaders">
+ <loader
+ class="org.eclipse.jet.examples.jdt.loaders.JavaLoader"
+ dynamicTypes="false"
+ id="java"
+ name="Java DOM loader"/>
+ </extension>
+ <extension
+ point="org.eclipse.jet.modelInspectors">
+ <inspector class="org.eclipse.jet.examples.jdt.inspectors.InspectASTNode">
+ <inspects class="org.eclipse.jdt.core.dom.ASTNode"/>
+ </inspector>
+ <inspector class="org.eclipse.jet.examples.jdt.inspectors.InspectASTNodeAttribute">
+ <inspects class="org.eclipse.jet.examples.jdt.inspectors.ASTNodeAttribute"/>
+ </inspector>
+ <inspector class="org.eclipse.jet.examples.jdt.inspectors.InspectASTNodeDocumentRoot">
+ <inspects class="org.eclipse.jet.examples.jdt.inspectors.ASTNodeDocumentRoot"/>
+ </inspector>
+ <inspector class="org.eclipse.jet.examples.jdt.inspectors.InspectASTNodeType">
+ <inspects class="org.eclipse.jet.examples.jdt.inspectors.ASTNodeType"/>
+ </inspector>
+ </extension>
+
+</plugin>
diff --git a/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/Activator.java b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/Activator.java
new file mode 100644
index 0000000..bf9dc1c
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/Activator.java
@@ -0,0 +1,63 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ */
+package org.eclipse.jet.examples.jdt;
+
+import org.eclipse.core.runtime.Plugin;
+import org.osgi.framework.BundleContext;
+
+/**
+ * The activator class controls the plug-in life cycle
+ */
+public class Activator extends Plugin {
+
+ // The plug-in ID
+ public static final String PLUGIN_ID = "org.eclipse.jet.examples.jdt";
+
+ // The shared instance
+ private static Activator plugin;
+
+ /**
+ * The constructor
+ */
+ public Activator() {
+ plugin = this;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
+ */
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
+ */
+ public void stop(BundleContext context) throws Exception {
+ plugin = null;
+ super.stop(context);
+ }
+
+ /**
+ * Returns the shared instance
+ *
+ * @return the shared instance
+ */
+ public static Activator getDefault() {
+ return plugin;
+ }
+
+}
diff --git a/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeAttribute.java b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeAttribute.java
new file mode 100644
index 0000000..9127538
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeAttribute.java
@@ -0,0 +1,77 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ */
+package org.eclipse.jet.examples.jdt.inspectors;
+
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.SimplePropertyDescriptor;
+
+/**
+ * Wrap ASTNode simple properties as XPath attribute objects
+ *
+ */
+public class ASTNodeAttribute {
+
+ /**
+ * The AST Node containing the property
+ */
+ private final ASTNode astNode;
+
+ /**
+ * The AST property descriptor
+ */
+ private final SimplePropertyDescriptor simplePropertyDescriptor;
+
+ /**
+ * Create an AST Node Attribute from an AST Node and property descriptor
+ * @param astNode the AST Node
+ * @param spd the property descriptor
+ */
+ public ASTNodeAttribute(final ASTNode astNode, final SimplePropertyDescriptor spd) {
+ this.astNode = astNode;
+ this.simplePropertyDescriptor = spd;
+ }
+
+ /**
+ * Return the XPath name of the AST Node
+ * @return the XPath name
+ */
+ public String getName() {
+ return simplePropertyDescriptor.getId();
+ }
+
+ /**
+ * Return the XPath string value of the property
+ * @return the XPath string value
+ */
+ public String stringValue() {
+ final Object value = astNode.getStructuralProperty(simplePropertyDescriptor);
+ return value != null ? value.toString() : "";
+ }
+
+ /**
+ * Return the XPath parent of the property
+ * @return the XPath parent (the ASTNode)
+ */
+ public Object getParent() {
+ return astNode;
+ }
+
+ /**
+ * Return the XPath document root of the property
+ * @return the document root object.
+ */
+ public Object getDocumentRoot() {
+ return ASTNodeDocumentRoot.documentRootFor(astNode);
+ }
+}
diff --git a/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeDocumentRoot.java b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeDocumentRoot.java
new file mode 100644
index 0000000..c0e7c71
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeDocumentRoot.java
@@ -0,0 +1,117 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ */
+package org.eclipse.jet.examples.jdt.inspectors;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.ASTParser;
+
+/**
+ * Wrap an root {@link ASTNode} as XPath document root.
+ *
+ */
+public class ASTNodeDocumentRoot {
+
+ /**
+ * The root node of the AST
+ */
+ private final ASTNode rootNode;
+ /**
+ * The Java source
+ */
+ private final String source;
+
+ /**
+ * A weak map of document roots by AST object.
+ */
+ private static final Map documentRootsByAST = Collections.synchronizedMap(new WeakHashMap());
+
+ /**
+ * Returns the {@link ASTNodeDocumentRoot} for an {@link ASTNode}, if one exists.
+ * @param astNode an ASTNode
+ * @return the document root or <code>null</code>
+ */
+ static ASTNodeDocumentRoot documentRootFor(final ASTNode astNode) {
+ return (ASTNodeDocumentRoot) documentRootsByAST.get(astNode.getAST());
+ }
+
+ /**
+ * Construct a document root from a root {@link ASTNode} and the source for the model.
+ * @param rootNode the root ASTNode (as returned by {@link ASTParser#createAST(org.eclipse.core.runtime.IProgressMonitor)}
+ * @param source the source for the AST
+ */
+ public ASTNodeDocumentRoot(final ASTNode rootNode, final String source) {
+ this.rootNode = rootNode;
+ this.source = source;
+
+ documentRootsByAST.put(rootNode.getAST(), this);
+ }
+
+ /**
+ * Return the AST root node
+ * @return the root node
+ */
+ public ASTNode getRootNode() {
+ return rootNode;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode() {
+ final int PRIME = 31;
+ int result = 1;
+ result = PRIME * result + ((rootNode == null) ? 0 : rootNode.hashCode());
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals(final Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final ASTNodeDocumentRoot other = (ASTNodeDocumentRoot) obj;
+ if (rootNode == null) {
+ if (other.rootNode != null)
+ return false;
+ } else if (!rootNode.equals(other.rootNode))
+ return false;
+ return true;
+ }
+
+ /**
+ * Return the XPath name of the root node.
+ * @return the XPath name (an empty string)
+ */
+ public String getName() {
+ return "";
+ }
+
+ /**
+ * Return the XPath string value of the root node
+ * @return The string value of the AST (the source code)
+ */
+ public String stringValue() {
+ return source != null ? source : "";
+ }
+
+}
diff --git a/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeType.java b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeType.java
new file mode 100644
index 0000000..61d498d
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/ASTNodeType.java
@@ -0,0 +1,103 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ */
+package org.eclipse.jet.examples.jdt.inspectors;
+
+import org.eclipse.jdt.core.dom.ASTNode;
+
+/**
+ * Represent the type of an {@link ASTNode} as an object suitable for use as an XPath attribute.
+ *
+ */
+public class ASTNodeType {
+
+ /**
+ * The ASTNode
+ */
+ private final ASTNode astNode;
+
+ /**
+ * Create an ASTNodeType from the ASTNode containing it.
+ * @param astNode
+ */
+ public ASTNodeType(final ASTNode astNode) {
+ this.astNode = astNode;
+
+ }
+
+ /**
+ * Return the XPath name of the attribute
+ * @return the string "nodeType"
+ */
+ public String getName() {
+ return "nodeType";
+ }
+
+ /**
+ * Return the XPath string value of the node type
+ * @return a name
+ */
+ public String stringValue() {
+ final String name = ASTNode.nodeClassForType(astNode.getNodeType()).getName();
+ final int lastDot = name.lastIndexOf('.');
+
+ return name.substring(lastDot + 1);
+ }
+
+ /**
+ * Return the XPath parent of the node type attribute
+ * @return the AST Node
+ */
+ public ASTNode getParent() {
+ return astNode;
+ }
+
+ /**
+ * Return the XPath document root of the node type attribute
+ * @return an object
+ */
+ public Object getDocumentRoot() {
+ return ASTNodeDocumentRoot.documentRootFor(astNode);
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode() {
+ final int PRIME = 31;
+ int result = 1;
+ result = PRIME * result + ((astNode == null) ? 0 : astNode.hashCode());
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals(final Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final ASTNodeType other = (ASTNodeType) obj;
+ if (astNode == null) {
+ if (other.astNode != null)
+ return false;
+ } else if (!astNode.equals(other.astNode))
+ return false;
+ return true;
+ }
+
+
+}
diff --git a/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNode.java b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNode.java
new file mode 100644
index 0000000..8321487
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNode.java
@@ -0,0 +1,330 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ */
+package org.eclipse.jet.examples.jdt.inspectors;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.SimplePropertyDescriptor;
+import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
+import org.eclipse.jet.xpath.inspector.AddElementException;
+import org.eclipse.jet.xpath.inspector.CopyElementException;
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.IElementInspector;
+import org.eclipse.jet.xpath.inspector.INodeInspector;
+import org.eclipse.jet.xpath.inspector.INodeInspectorExtension1;
+import org.eclipse.jet.xpath.inspector.InvalidChildException;
+import org.eclipse.jet.xpath.inspector.SimpleElementRequiresValueException;
+
+/**
+ * Inspect ASTNode objects for the JET XPath engine. Realizes ASTNodes as XPath elements.
+ *
+ */
+public class InspectASTNode implements INodeInspector, IElementInspector, INodeInspectorExtension1 {
+
+ private static final Map propertyDescriptorByType = new HashMap(50);
+ private static final Map attributePropertyDescriptorByType = new HashMap(50);
+ private static final Map childPropertyDescriptorByType = new HashMap(50);
+
+ /**
+ * Return the named property descriptor for the given node.
+ * @param node
+ * @param id
+ * @return the property descriptor or null, if id is not know property
+ */
+ private StructuralPropertyDescriptor getPropertyDescriptor(final ASTNode node, final String id) {
+ final List propsList = getPropertyDescriptors(node);
+
+ for (final Iterator i = propsList.iterator(); i.hasNext();) {
+ final StructuralPropertyDescriptor spd = (StructuralPropertyDescriptor) i.next();
+ if(spd.getId().equals(id)) {
+ return spd;
+ }
+ }
+
+
+ return null;
+ }
+
+ /**
+ * Return the property descriptors for the given type.
+ * @param node
+ * @return
+ */
+ private List getPropertyDescriptors(final ASTNode node) {
+ final Integer nodeType = Integer.valueOf(node.getNodeType());
+
+ List propsList = (List)propertyDescriptorByType.get(nodeType);
+
+ if(propsList == null) {
+ synchronized (propertyDescriptorByType) {
+ propsList = (List)propertyDescriptorByType.get(nodeType);
+ if(propsList == null) {
+ propsList = node.structuralPropertiesForType();
+ propertyDescriptorByType.put(nodeType, propsList);
+ }
+
+ }
+ }
+ return propsList;
+ }
+
+ /**
+ * @param node
+ * @return
+ */
+ private List getAttributePropertyDescriptors(final ASTNode node) {
+ final Integer nodeType = Integer.valueOf(node.getNodeType());
+
+ List propsList = (List)attributePropertyDescriptorByType.get(nodeType);
+ if(propsList == null) {
+ synchronized (attributePropertyDescriptorByType) {
+ propsList = (List)attributePropertyDescriptorByType.get(nodeType);
+ if(propsList == null) {
+ final List allPropsList = node.structuralPropertiesForType();
+ propsList = new ArrayList(allPropsList.size());
+ for (final Iterator i = allPropsList.iterator(); i
+ .hasNext();) {
+ final StructuralPropertyDescriptor spd = (StructuralPropertyDescriptor) i.next();
+ if(spd.isSimpleProperty()) {
+ propsList.add(spd);
+ }
+
+ }
+ attributePropertyDescriptorByType.put(nodeType, propsList);
+ }
+
+ }
+ }
+ return propsList;
+ }
+
+ /**
+ * @param node
+ * @return
+ */
+ private List getElementPropertyDescriptors(final ASTNode node) {
+ final Integer nodeType = Integer.valueOf(node.getNodeType());
+
+ List propsList = (List)childPropertyDescriptorByType.get(nodeType);
+ if(propsList == null) {
+ synchronized (childPropertyDescriptorByType) {
+ propsList = (List)childPropertyDescriptorByType.get(nodeType);
+ if(propsList == null) {
+ final List allPropsList = node.structuralPropertiesForType();
+ propsList = new ArrayList(allPropsList.size());
+ for (final Iterator i = allPropsList.iterator(); i
+ .hasNext();) {
+ final StructuralPropertyDescriptor spd = (StructuralPropertyDescriptor) i.next();
+ if(!spd.isSimpleProperty()) {
+ propsList.add(spd);
+ }
+
+ }
+ childPropertyDescriptorByType.put(nodeType, propsList);
+ }
+
+ }
+ }
+ return propsList;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#expandedNameOf(java.lang.Object)
+ */
+ public ExpandedName expandedNameOf(final Object node) {
+ return new ExpandedName(nameOf(node));
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getChildren(java.lang.Object)
+ */
+ public Object[] getChildren(final Object node) {
+ final ASTNode astNode = (ASTNode) node;
+ final List spds = getElementPropertyDescriptors(astNode);
+ final List children = new ArrayList(spds.size());
+ for (final Iterator i = spds.iterator(); i.hasNext();) {
+ final StructuralPropertyDescriptor spd = (StructuralPropertyDescriptor) i.next();
+ if(spd.isChildProperty()) {
+ children.add(astNode.getStructuralProperty(spd));
+ } else if(spd.isChildListProperty()) {
+ children.addAll((List)astNode.getStructuralProperty(spd));
+ }
+ }
+ return children.toArray();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getDocumentRoot(java.lang.Object)
+ */
+ public Object getDocumentRoot(final Object node) {
+ final ASTNode astNode = (ASTNode) node;
+ return ASTNodeDocumentRoot.documentRootFor(astNode);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getNodeKind(java.lang.Object)
+ */
+ public NodeKind getNodeKind(final Object node) {
+ return NodeKind.ELEMENT;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getParent(java.lang.Object)
+ */
+ public Object getParent(final Object node) {
+ final ASTNode astNode = (ASTNode) node;
+ final ASTNode parent = astNode.getParent();
+ return parent == null ? ASTNodeDocumentRoot.documentRootFor(astNode) : parent;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#nameOf(java.lang.Object)
+ */
+ public String nameOf(final Object node) {
+ final ASTNode astNode = (ASTNode) node;
+ final StructuralPropertyDescriptor locationInParent = astNode.getLocationInParent();
+ if(locationInParent != null) {
+ return locationInParent.getId();
+ } else {
+ return ASTNode.nodeClassForType(astNode.getNodeType()).getName();
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#stringValueOf(java.lang.Object)
+ */
+ public String stringValueOf(final Object node) {
+ final ASTNode astNode = (ASTNode) node;
+ final ASTNodeDocumentRoot documentRoot = ASTNodeDocumentRoot.documentRootFor(astNode);
+
+ final int start = astNode.getStartPosition();
+ final int end = start + astNode.getLength();
+ return documentRoot.stringValue().substring(start, end);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#testExpandedName(java.lang.Object, org.eclipse.jet.xpath.inspector.ExpandedName)
+ */
+ public boolean testExpandedName(final Object node, final ExpandedName testName) {
+ return testName.equals(expandedNameOf(node));
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.IElementInspector#addElement(java.lang.Object, org.eclipse.jet.xpath.inspector.ExpandedName, java.lang.Object)
+ */
+ public Object addElement(final Object node, final ExpandedName elementName,
+ final Object addBeforeThisSibling)
+ throws SimpleElementRequiresValueException, InvalidChildException {
+ throw new UnsupportedOperationException();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.IElementInspector#addTextElement(java.lang.Object, java.lang.String, java.lang.String, boolean)
+ */
+ public Object addTextElement(final Object parentElement, final String name,
+ final String bodyContent, final boolean asCData) throws AddElementException {
+ throw new UnsupportedOperationException();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.IElementInspector#copyElement(java.lang.Object, java.lang.Object, java.lang.String, boolean)
+ */
+ public Object copyElement(final Object tgtParent, final Object srcElement, final String name,
+ final boolean recursive) throws CopyElementException {
+ throw new UnsupportedOperationException();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.IElementInspector#createAttribute(java.lang.Object, java.lang.String, java.lang.String)
+ */
+ public boolean createAttribute(final Object node, final String attributeName,
+ final String value) {
+ return false;
+ }
+
+ @SuppressWarnings("unchecked")
+ public Object[] getAttributes(final Object node) {
+ final ASTNode astNode = (ASTNode) node;
+ final List spds = getAttributePropertyDescriptors(astNode);
+ final List result = new ArrayList(spds.size() + 1);
+ result.add(new ASTNodeType(astNode));
+ for (final Iterator i = spds.iterator(); i.hasNext();) {
+ final SimplePropertyDescriptor spd = (SimplePropertyDescriptor) i.next();
+ result.add(new ASTNodeAttribute(astNode,spd));
+ }
+ return result.toArray();
+ }
+
+ public Object getNamedAttribute(final Object node,
+ final ExpandedName nameTestExpandedName) {
+ final ASTNode astNode = (ASTNode) node;
+
+ final String propertyName = nameTestExpandedName.getLocalPart();
+
+ if("nodeType".equals(propertyName)) {
+ return new ASTNodeType(astNode);
+ }
+
+ final StructuralPropertyDescriptor spd = getPropertyDescriptor(astNode, propertyName);
+
+ if(spd != null && spd.isSimpleProperty()) {
+ return new ASTNodeAttribute(astNode, (SimplePropertyDescriptor)spd);
+ }
+
+
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.IElementInspector#removeAttribute(java.lang.Object, java.lang.String)
+ */
+ public void removeAttribute(final Object node, final String name) {
+ throw new UnsupportedOperationException();
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.IElementInspector#removeElement(java.lang.Object)
+ */
+ public void removeElement(final Object node) {
+ throw new UnsupportedOperationException();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspectorExtension1#getNamedChildren(java.lang.Object, org.eclipse.jet.xpath.inspector.ExpandedName)
+ */
+ public Object[] getNamedChildren(final Object node, final ExpandedName nameTestExpandedName) {
+ final ASTNode astNode = (ASTNode) node;
+ final String propertyName = nameTestExpandedName.getLocalPart();
+ final StructuralPropertyDescriptor spd = getPropertyDescriptor(astNode, propertyName);
+
+ if(spd == null) {
+ return new Object[0];
+ }
+ if(spd.isChildProperty()) {
+ return new Object[] {astNode.getStructuralProperty(spd)};
+ } else if(spd.isChildListProperty()) {
+ return ((List)astNode.getStructuralProperty(spd)).toArray();
+ }
+
+
+ return new Object[0];
+ }
+
+}
diff --git a/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeAttribute.java b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeAttribute.java
new file mode 100644
index 0000000..d6b28db
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeAttribute.java
@@ -0,0 +1,89 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ */
+package org.eclipse.jet.examples.jdt.inspectors;
+
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.INodeInspector;
+
+/**
+ * Inspect {@link ASTNodeAttribute} for the JET XPath engine. Realizes these
+ * objects as XPath attributes.
+ *
+ */
+public class InspectASTNodeAttribute implements INodeInspector {
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#expandedNameOf(java.lang.Object)
+ */
+ public ExpandedName expandedNameOf(final Object node) {
+ return new ExpandedName(nameOf(node));
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getChildren(java.lang.Object)
+ */
+ public Object[] getChildren(final Object node) {
+ return new Object[0];
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getDocumentRoot(java.lang.Object)
+ */
+ public Object getDocumentRoot(final Object node) {
+ final ASTNodeAttribute astNodeAttribute = (ASTNodeAttribute) node;
+ return astNodeAttribute.getDocumentRoot();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getNodeKind(java.lang.Object)
+ */
+ public NodeKind getNodeKind(final Object node) {
+ return NodeKind.ATTRIBUTE;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getParent(java.lang.Object)
+ */
+ public Object getParent(final Object node) {
+ final ASTNodeAttribute astNodeAttribute = (ASTNodeAttribute) node;
+
+ return astNodeAttribute.getParent();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#nameOf(java.lang.Object)
+ */
+ public String nameOf(final Object node) {
+ final ASTNodeAttribute astNodeAttribute = (ASTNodeAttribute) node;
+
+ return astNodeAttribute.getName();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#stringValueOf(java.lang.Object)
+ */
+ public String stringValueOf(final Object node) {
+ final ASTNodeAttribute astNodeAttribute = (ASTNodeAttribute) node;
+
+ return astNodeAttribute.stringValue();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#testExpandedName(java.lang.Object, org.eclipse.jet.xpath.inspector.ExpandedName)
+ */
+ public boolean testExpandedName(final Object node, final ExpandedName testName) {
+ return testName.equals(expandedNameOf(node));
+ }
+
+}
diff --git a/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeDocumentRoot.java b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeDocumentRoot.java
new file mode 100644
index 0000000..7c81d5f
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeDocumentRoot.java
@@ -0,0 +1,86 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ */
+package org.eclipse.jet.examples.jdt.inspectors;
+
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.INodeInspector;
+
+/**
+ * Inspect {@link ASTNodeDocumentRoot} objects for the JET XPath engine
+ *
+ */
+public class InspectASTNodeDocumentRoot implements INodeInspector {
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#expandedNameOf(java.lang.Object)
+ */
+ public ExpandedName expandedNameOf(final Object node) {
+ return new ExpandedName(nameOf(node));
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getChildren(java.lang.Object)
+ */
+ public Object[] getChildren(final Object node) {
+ final ASTNodeDocumentRoot astDocumentRoot = (ASTNodeDocumentRoot) node;
+
+ return new Object[] {astDocumentRoot.getRootNode()};
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getDocumentRoot(java.lang.Object)
+ */
+ public Object getDocumentRoot(final Object node) {
+ return node;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getNodeKind(java.lang.Object)
+ */
+ public NodeKind getNodeKind(final Object obj) {
+ return NodeKind.ROOT;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getParent(java.lang.Object)
+ */
+ public Object getParent(final Object obj) {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#nameOf(java.lang.Object)
+ */
+ public String nameOf(final Object node) {
+ final ASTNodeDocumentRoot astDocumentRoot = (ASTNodeDocumentRoot) node;
+
+ return astDocumentRoot.getName();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#stringValueOf(java.lang.Object)
+ */
+ public String stringValueOf(final Object node) {
+ final ASTNodeDocumentRoot astDocumentRoot = (ASTNodeDocumentRoot) node;
+ return astDocumentRoot.stringValue();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#testExpandedName(java.lang.Object, org.eclipse.jet.xpath.inspector.ExpandedName)
+ */
+ public boolean testExpandedName(final Object node, final ExpandedName testName) {
+ return testName.equals(expandedNameOf(node));
+ }
+
+}
diff --git a/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeType.java b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeType.java
new file mode 100644
index 0000000..980f03e
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/inspectors/InspectASTNodeType.java
@@ -0,0 +1,85 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ */
+package org.eclipse.jet.examples.jdt.inspectors;
+
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.INodeInspector;
+
+/**
+ * Realize {@link ASTNodeType} as XPath attribute objects.
+ *
+ */
+public class InspectASTNodeType implements INodeInspector {
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#expandedNameOf(java.lang.Object)
+ */
+ public ExpandedName expandedNameOf(final Object node) {
+ return new ExpandedName(nameOf(node));
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getChildren(java.lang.Object)
+ */
+ public Object[] getChildren(final Object node) {
+ return new Object[0];
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getDocumentRoot(java.lang.Object)
+ */
+ public Object getDocumentRoot(final Object node) {
+ final ASTNodeType nodeTypeAttribute = (ASTNodeType) node;
+ return nodeTypeAttribute.getDocumentRoot();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getNodeKind(java.lang.Object)
+ */
+ public NodeKind getNodeKind(final Object obj) {
+ return NodeKind.ATTRIBUTE;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getParent(java.lang.Object)
+ */
+ public Object getParent(final Object node) {
+ final ASTNodeType nodeTypeAttribute = (ASTNodeType) node;
+ return nodeTypeAttribute.getDocumentRoot();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#nameOf(java.lang.Object)
+ */
+ public String nameOf(final Object node) {
+ final ASTNodeType nodeTypeAttribute = (ASTNodeType) node;
+ return nodeTypeAttribute.getName();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#stringValueOf(java.lang.Object)
+ */
+ public String stringValueOf(final Object node) {
+ final ASTNodeType nodeTypeAttribute = (ASTNodeType) node;
+ return nodeTypeAttribute.stringValue();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#testExpandedName(java.lang.Object, org.eclipse.jet.xpath.inspector.ExpandedName)
+ */
+ public boolean testExpandedName(final Object node, final ExpandedName testName) {
+ return testName.equals(expandedNameOf(node));
+ }
+
+}
diff --git a/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/loaders/JavaLoader.java b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/loaders/JavaLoader.java
new file mode 100644
index 0000000..29ee825
--- /dev/null
+++ b/examples/org.eclipse.jet.examples.jdt/src/org/eclipse/jet/examples/jdt/loaders/JavaLoader.java
@@ -0,0 +1,87 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ */
+package org.eclipse.jet.examples.jdt.loaders;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.ASTParser;
+import org.eclipse.jet.examples.jdt.inspectors.ASTNodeDocumentRoot;
+import org.eclipse.jet.runtime.model.IModelLoader;
+
+/**
+ * Load a java file as an JDT Abstract syntax tree.
+ *
+ */
+public class JavaLoader implements IModelLoader {
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.runtime.model.IModelLoader#canLoad(java.lang.String)
+ */
+ public boolean canLoad(String kind) {
+
+ return "java".equals(kind);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.runtime.model.IModelLoader#load(java.net.URL)
+ */
+ public Object load(URL modelUrl) throws IOException {
+ return load(modelUrl, "java");
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.runtime.model.IModelLoader#load(java.net.URL, java.lang.String)
+ */
+ public Object load(URL modelUrl, String kind) throws IOException {
+ InputStreamReader reader = new InputStreamReader(new BufferedInputStream(modelUrl.openStream()));
+ StringBuffer buffer = new StringBuffer(512);
+ try {
+ char[] cbuf = new char[512];
+ int i;
+ while( (i = reader.read(cbuf)) > 0 ) {
+ buffer.append(cbuf, 0, i);
+ }
+
+ } finally {
+ try {
+ reader.close();
+ } catch(IOException e) {
+ // ignore close exceptiosn
+ }
+ }
+ return loadFromString(buffer.toString(), kind);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.runtime.model.IModelLoader#loadFromString(java.lang.String, java.lang.String)
+ */
+ public Object loadFromString(String serializedModel, String kind)
+ throws IOException {
+ ASTParser astParser = ASTParser.newParser(AST.JLS3);
+
+ astParser.setSource(serializedModel.toCharArray());
+
+ astParser.setKind(ASTParser.K_COMPILATION_UNIT);
+
+ ASTNode astNode = astParser.createAST(new NullProgressMonitor());
+ return new ASTNodeDocumentRoot(astNode, serializedModel);
+ }
+
+}

Back to the top