Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2009-03-16 15:20:26 -0400
committerpelder2009-03-16 15:20:26 -0400
commitad1d6c4e28a3adde902036817b49025e438581d9 (patch)
tree4bb753d6e7151530efe1d7041f5f5d0e122a042e
parent68597a7cc2cbac795a42be1c36cd7f0ae35a691a (diff)
downloadorg.eclipse.jet-ad1d6c4e28a3adde902036817b49025e438581d9.tar.gz
org.eclipse.jet-ad1d6c4e28a3adde902036817b49025e438581d9.tar.xz
org.eclipse.jet-ad1d6c4e28a3adde902036817b49025e438581d9.zip
[268857] Allow JET to navigate JDT abstract syntax trees
-rw-r--r--plugins/org.eclipse.jet/plugin.xml33
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/model/jdt/JavaLoader.java150
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/ASTNodeDocumentRoot.java117
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectASTNode.java345
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectASTNodeDocumentRoot.java86
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectNonDescriptorAttribute.java84
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectSimplePropertyDescriptorAttribute.java89
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/NonDescriptorAttribute.java177
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/SimplePropertyDescriptorAttribute.java77
9 files changed, 1158 insertions, 0 deletions
diff --git a/plugins/org.eclipse.jet/plugin.xml b/plugins/org.eclipse.jet/plugin.xml
index b4a59b8..a60f533 100644
--- a/plugins/org.eclipse.jet/plugin.xml
+++ b/plugins/org.eclipse.jet/plugin.xml
@@ -1343,6 +1343,15 @@ See the documentation for the Java class java.util.Locale for details on languag
id="xml"
name="%extensions.modelLoaders.xml.name">
</loader>
+ <loader
+ class="org.eclipse.jet.internal.runtime.model.jdt.JavaLoader"
+ dynamicTypes="false"
+ id="java"
+ name="Java AST Loader">
+ <type
+ fileType="java">
+ </type>
+ </loader>
</extension>
<extension
point="org.eclipse.jet.modelInspectors">
@@ -1373,6 +1382,30 @@ See the documentation for the Java class java.util.Locale for details on languag
<inspector class="org.eclipse.jet.internal.xpath.inspectors.PropertiesInspector">
<inspects class="java.util.Properties"/>
</inspector>
+ <inspector
+ class="org.eclipse.jet.internal.xpath.inspectors.jdt.InspectASTNode">
+ <inspects
+ class="org.eclipse.jdt.core.dom.ASTNode">
+ </inspects>
+ </inspector>
+ <inspector
+ class="org.eclipse.jet.internal.xpath.inspectors.jdt.InspectASTNodeDocumentRoot">
+ <inspects
+ class="org.eclipse.jet.internal.xpath.inspectors.jdt.ASTNodeDocumentRoot">
+ </inspects>
+ </inspector>
+ <inspector
+ class="org.eclipse.jet.internal.xpath.inspectors.jdt.InspectNonDescriptorAttribute">
+ <inspects
+ class="org.eclipse.jet.internal.xpath.inspectors.jdt.NonDescriptorAttribute">
+ </inspects>
+ </inspector>
+ <inspector
+ class="org.eclipse.jet.internal.xpath.inspectors.jdt.InspectSimplePropertyDescriptorAttribute">
+ <inspects
+ class="org.eclipse.jet.internal.xpath.inspectors.jdt.SimplePropertyDescriptorAttribute">
+ </inspects>
+ </inspector>
</extension>
<extension
id="taskmarker"
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/model/jdt/JavaLoader.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/model/jdt/JavaLoader.java
new file mode 100644
index 0000000..d3636ab
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/model/jdt/JavaLoader.java
@@ -0,0 +1,150 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007, 2009 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.internal.runtime.model.jdt;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+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.internal.xpath.inspectors.jdt.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-NLS-1$
+ }
+
+ /*
+ * (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-NLS-1$
+ }
+
+ /*
+ * (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 {
+ if ("platform".equals(modelUrl.getProtocol())) { //$NON-NLS-1$
+ String path = modelUrl.getPath();
+ if (path != null && path.startsWith("/resource/")) { //$NON-NLS-1$
+ final IPath ipath = new Path(path).removeFirstSegments(1);
+ final IResource resource = ResourcesPlugin.getWorkspace()
+ .getRoot().findMember(ipath);
+ if (resource != null) {
+ final IJavaElement javaElement = JavaCore.create(resource);
+ if (javaElement instanceof ICompilationUnit) {
+ final ASTParser astParser = ASTParser
+ .newParser(AST.JLS3);
+ astParser.setSource((ICompilationUnit) javaElement);
+ final ASTNode astNode = astParser
+ .createAST(new NullProgressMonitor());
+ try {
+ final String contents = ((ICompilationUnit) javaElement)
+ .getBuffer().getContents();
+ return new ASTNodeDocumentRoot(astNode, contents);
+ } catch (JavaModelException e) {
+ // ignore, ... just fall through;
+ }
+ }
+ }
+ }
+ }
+ 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(getKind(kind));
+
+ ASTNode astNode = astParser.createAST(new NullProgressMonitor());
+ return new ASTNodeDocumentRoot(astNode, serializedModel);
+ }
+
+ /**
+ * @param kind
+ * @return
+ */
+ private int getKind(String kind) {
+ if ("classbodydeclarations".equalsIgnoreCase(kind)) { //$NON-NLS-1$
+ return ASTParser.K_CLASS_BODY_DECLARATIONS;
+ } else if ("expression".equals(kind)) { //$NON-NLS-1$
+ return ASTParser.K_EXPRESSION;
+
+ } else if ("statements".equals(kind)) { //$NON-NLS-1$
+ return ASTParser.K_STATEMENTS;
+ } else {
+ return ASTParser.K_COMPILATION_UNIT;
+ }
+
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/ASTNodeDocumentRoot.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/ASTNodeDocumentRoot.java
new file mode 100644
index 0000000..28f019a
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/ASTNodeDocumentRoot.java
@@ -0,0 +1,117 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007, 2009 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.internal.xpath.inspectors.jdt;
+
+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 ""; //$NON-NLS-1$
+ }
+
+ /**
+ * 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 : ""; //$NON-NLS-1$
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectASTNode.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectASTNode.java
new file mode 100644
index 0000000..c86ef83
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectASTNode.java
@@ -0,0 +1,345 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007, 2009 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.internal.xpath.inspectors.jdt;
+
+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 = new Integer(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 = new Integer(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 = new Integer(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();
+ final Object structuralProperty = astNode.getStructuralProperty(spd);
+ if(spd.isChildProperty() && structuralProperty != null) {
+ children.add(structuralProperty);
+ } else if(spd.isChildListProperty() && structuralProperty != null) {
+ final List spList = (List)structuralProperty;
+ children.addAll(spList);
+ }
+ }
+ 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 ? (Object)ASTNodeDocumentRoot.documentRootFor(astNode) : (Object)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 {
+ final String fullyQualifiedName = ASTNode.nodeClassForType(astNode.getNodeType()).getName();
+ final int lastDot = fullyQualifiedName.lastIndexOf('.');
+ final StringBuffer simpleName = new StringBuffer(lastDot >= 0 ? fullyQualifiedName.substring(lastDot + 1) : fullyQualifiedName);
+ simpleName.setCharAt(0, Character.toLowerCase(simpleName.charAt(0)));
+ return simpleName.toString();
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#stringValueOf(java.lang.Object)
+ */
+ public String stringValueOf(final Object node) {
+ final ASTNode astNode = (ASTNode) node;
+ return getSource(astNode);
+ }
+
+ /**
+ * @param astNode
+ * @return
+ */
+ private String getSource(final ASTNode astNode) {
+ 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;
+ }
+
+ public Object[] getAttributes(final Object node) {
+ final ASTNode astNode = (ASTNode) node;
+ final List spds = getAttributePropertyDescriptors(astNode);
+ final List nonspds = NonDescriptorAttribute.getAttributes(astNode);
+ final List result = new ArrayList(spds.size() + nonspds.size());
+ result.addAll(nonspds);
+ for (final Iterator i = spds.iterator(); i.hasNext();) {
+ final SimplePropertyDescriptor spd = (SimplePropertyDescriptor) i.next();
+ result.add(new SimplePropertyDescriptorAttribute(astNode,spd));
+ }
+ return result.toArray();
+ }
+
+ public Object getNamedAttribute(final Object node,
+ final ExpandedName nameTestExpandedName) {
+ final ASTNode astNode = (ASTNode) node;
+
+ final String propertyName = nameTestExpandedName.getLocalPart();
+
+ final NonDescriptorAttribute attribute = NonDescriptorAttribute.getAttribute(astNode, propertyName);
+ if(attribute != null) {
+ return attribute;
+ }
+
+ final StructuralPropertyDescriptor spd = getPropertyDescriptor(astNode, propertyName);
+
+ if(spd != null && spd.isSimpleProperty()) {
+ return new SimplePropertyDescriptorAttribute(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/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectASTNodeDocumentRoot.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectASTNodeDocumentRoot.java
new file mode 100644
index 0000000..ddc31af
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectASTNodeDocumentRoot.java
@@ -0,0 +1,86 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007, 2009 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.internal.xpath.inspectors.jdt;
+
+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/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectNonDescriptorAttribute.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectNonDescriptorAttribute.java
new file mode 100644
index 0000000..9fda61a
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectNonDescriptorAttribute.java
@@ -0,0 +1,84 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2009 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.internal.xpath.inspectors.jdt;
+
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.INodeInspector;
+
+/**
+ * @author pelder
+ *
+ */
+public class InspectNonDescriptorAttribute implements INodeInspector {
+
+ private static final Object[] NULL_OBJECT_ARRAY = new Object[0];
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#expandedNameOf(java.lang.Object)
+ */
+ public ExpandedName expandedNameOf(Object node) {
+ return new ExpandedName(nameOf(node));
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getChildren(java.lang.Object)
+ */
+ public Object[] getChildren(Object node) {
+ return NULL_OBJECT_ARRAY;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getDocumentRoot(java.lang.Object)
+ */
+ public Object getDocumentRoot(Object node) {
+ return ASTNodeDocumentRoot.documentRootFor((ASTNode) getParent(node));
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getNodeKind(java.lang.Object)
+ */
+ public NodeKind getNodeKind(Object obj) {
+ return NodeKind.ATTRIBUTE;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#getParent(java.lang.Object)
+ */
+ public Object getParent(Object obj) {
+ return ((NonDescriptorAttribute)obj).getParent();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#nameOf(java.lang.Object)
+ */
+ public String nameOf(Object node) {
+ return ((NonDescriptorAttribute)node).getName();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#stringValueOf(java.lang.Object)
+ */
+ public String stringValueOf(Object node) {
+ return ((NonDescriptorAttribute)node).getValue();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#testExpandedName(java.lang.Object, org.eclipse.jet.xpath.inspector.ExpandedName)
+ */
+ public boolean testExpandedName(Object node, ExpandedName testName) {
+ return expandedNameOf(node).equals(testName);
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectSimplePropertyDescriptorAttribute.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectSimplePropertyDescriptorAttribute.java
new file mode 100644
index 0000000..0c1db7c
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/InspectSimplePropertyDescriptorAttribute.java
@@ -0,0 +1,89 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007, 2009 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.internal.xpath.inspectors.jdt;
+
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.INodeInspector;
+
+/**
+ * Inspect {@link SimplePropertyDescriptorAttribute} for the JET XPath engine. Realizes these
+ * objects as XPath attributes.
+ *
+ */
+public class InspectSimplePropertyDescriptorAttribute 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 SimplePropertyDescriptorAttribute astNodeAttribute = (SimplePropertyDescriptorAttribute) 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 SimplePropertyDescriptorAttribute astNodeAttribute = (SimplePropertyDescriptorAttribute) node;
+
+ return astNodeAttribute.getParent();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#nameOf(java.lang.Object)
+ */
+ public String nameOf(final Object node) {
+ final SimplePropertyDescriptorAttribute astNodeAttribute = (SimplePropertyDescriptorAttribute) node;
+
+ return astNodeAttribute.getName();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.inspector.INodeInspector#stringValueOf(java.lang.Object)
+ */
+ public String stringValueOf(final Object node) {
+ final SimplePropertyDescriptorAttribute astNodeAttribute = (SimplePropertyDescriptorAttribute) 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/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/NonDescriptorAttribute.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/NonDescriptorAttribute.java
new file mode 100644
index 0000000..4675eae
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/NonDescriptorAttribute.java
@@ -0,0 +1,177 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2009 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.internal.xpath.inspectors.jdt;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.jdt.core.dom.ASTNode;
+
+/**
+ * @author pelder
+ *
+ */
+public abstract class NonDescriptorAttribute {
+
+ private static final String NODE_TYPE_ATTR = "nodeType"; //$NON-NLS-1$
+ private static final String SOURCE_ATTR = "source"; //$NON-NLS-1$
+ private static final String START_POSITION_ATTR = "startPosition"; //$NON-NLS-1$
+ private static final String LENGTH_ATTR = "length"; //$NON-NLS-1$
+
+ public static final class StartPosition extends NonDescriptorAttribute {
+
+ public StartPosition(ASTNode node) {
+ super(node);
+ }
+
+ public String getName() {
+ return START_POSITION_ATTR;
+ }
+
+ public String getValue() {
+ // TODO Auto-generated method stub
+ return Integer.toString(getParent().getStartPosition());
+ }
+
+ }
+
+ public static final class Length extends NonDescriptorAttribute {
+
+ public Length(ASTNode node) {
+ super(node);
+ }
+
+ public String getName() {
+ return LENGTH_ATTR;
+ }
+
+ public String getValue() {
+ return Integer.toString(getParent().getLength());
+ }
+ }
+
+ public static final class Source extends NonDescriptorAttribute {
+
+ public Source(ASTNode node) {
+ super(node);
+ }
+
+ public String getName() {
+ return SOURCE_ATTR;
+ }
+
+ public String getValue() {
+ final ASTNode astNode = getParent();
+ final ASTNodeDocumentRoot documentRoot = ASTNodeDocumentRoot.documentRootFor(astNode);
+
+ final int start = astNode.getStartPosition();
+ final int end = start + astNode.getLength();
+ return documentRoot.stringValue().substring(start, end);
+ }
+
+ }
+
+ public static final class NodeType extends NonDescriptorAttribute {
+
+ public NodeType(ASTNode node) {
+ super(node);
+ }
+
+ public String getName() {
+ return NODE_TYPE_ATTR;
+ }
+
+ public String getValue() {
+ final String name = ASTNode.nodeClassForType(getParent().getNodeType()).getName();
+ final int lastDot = name.lastIndexOf('.');
+
+ return name.substring(lastDot + 1);
+ }
+
+ }
+
+ public static NonDescriptorAttribute getAttribute(ASTNode astNode, String propertyName) {
+ if(LENGTH_ATTR.equals(propertyName)) {
+ return new NonDescriptorAttribute.Length(astNode);
+ } else if(START_POSITION_ATTR.equals(propertyName)) {
+ return new NonDescriptorAttribute.StartPosition(astNode);
+ } else if(SOURCE_ATTR.equals(propertyName)) {
+ return new NonDescriptorAttribute.Source(astNode);
+ } else if(NODE_TYPE_ATTR.equals(propertyName)) {
+ return new NonDescriptorAttribute.NodeType(astNode);
+ }
+ return null;
+ }
+
+ public static List getAttributes(ASTNode astNode) {
+ final List result = new ArrayList(4);
+ result.add(getAttribute(astNode, START_POSITION_ATTR));
+ result.add(getAttribute(astNode, LENGTH_ATTR));
+ result.add(getAttribute(astNode, SOURCE_ATTR));
+ result.add(getAttribute(astNode, NODE_TYPE_ATTR));
+ return result;
+ }
+ private final ASTNode node;
+
+ public NonDescriptorAttribute(ASTNode node) {
+ if(node == null) {
+ throw new NullPointerException();
+ }
+ this.node = node;
+
+ }
+
+ public ASTNode getParent() {
+ return node;
+ }
+
+ public abstract String getName();
+
+ public abstract String getValue();
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
+ result = prime * result + ((node == null) ? 0 : node.hashCode());
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (!(obj instanceof NonDescriptorAttribute)) {
+ return false;
+ }
+ NonDescriptorAttribute other = (NonDescriptorAttribute) obj;
+ if (!getName().equals(other.getName())) {
+ return false;
+ }
+ if (!node.equals(other.node)) {
+ return false;
+ }
+ return true;
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/SimplePropertyDescriptorAttribute.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/SimplePropertyDescriptorAttribute.java
new file mode 100644
index 0000000..0b99e4e
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/jdt/SimplePropertyDescriptorAttribute.java
@@ -0,0 +1,77 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2007, 2009 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.internal.xpath.inspectors.jdt;
+
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.SimplePropertyDescriptor;
+
+/**
+ * Wrap ASTNode simple properties as XPath attribute objects
+ *
+ */
+public class SimplePropertyDescriptorAttribute {
+
+ /**
+ * 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 SimplePropertyDescriptorAttribute(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() : ""; //$NON-NLS-1$
+ }
+
+ /**
+ * 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);
+ }
+}

Back to the top