Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2008-09-27 07:08:34 -0400
committerpelder2008-09-27 07:08:34 -0400
commitfb93077879d8e1b21981f74ccf184289d3ebdab7 (patch)
treea88f3b89f61326d72752548cf8dba0ca006f0196
parente915cd0b4c4b6b5bbe4febb37e783f531d817145 (diff)
downloadorg.eclipse.jet-fb93077879d8e1b21981f74ccf184289d3ebdab7.tar.gz
org.eclipse.jet-fb93077879d8e1b21981f74ccf184289d3ebdab7.tar.xz
org.eclipse.jet-fb93077879d8e1b21981f74ccf184289d3ebdab7.zip
[248713] [Regression] NPE in XPath engine
-rwxr-xr-xplugins/org.eclipse.jet.all-feature/feature.xml2
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/DOMInspector.java44
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/EObjectInspector.java260
-rw-r--r--tests/org.eclipse.jet.tests/.settings/org.eclipse.jdt.ui.prefs4
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/AbstractCopyElementTests.java82
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/AllTests.java14
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/EMFXMLCopyELementTests.java28
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/XMLDOMCopyElementTests.java28
8 files changed, 342 insertions, 120 deletions
diff --git a/plugins/org.eclipse.jet.all-feature/feature.xml b/plugins/org.eclipse.jet.all-feature/feature.xml
index 6526663..9826a23 100755
--- a/plugins/org.eclipse.jet.all-feature/feature.xml
+++ b/plugins/org.eclipse.jet.all-feature/feature.xml
@@ -2,7 +2,7 @@
<feature
id="org.eclipse.jet.all"
label="%featureName"
- version="0.9.0.qualifier"
+ version="0.9.100.qualifier"
provider-name="%providerName">
<description>
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/DOMInspector.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/DOMInspector.java
index 8c305d1..9608f32 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/DOMInspector.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/DOMInspector.java
@@ -273,7 +273,11 @@ public class DOMInspector implements INodeInspector, IElementInspector
String nsURI = nameTestExpandedName.getNamespaceURI();
if (nsURI == null)
{
- return node.getAttributes().getNamedItem(nameTestExpandedName.getLocalPart());
+ Node namedItem = node.getAttributes().getNamedItem(nameTestExpandedName.getLocalPart());
+ if(namedItem == null) {
+ namedItem = node.getAttributes().getNamedItem(nameTestExpandedName.getLocalPart());
+ }
+ return namedItem;
}
else
{
@@ -336,18 +340,39 @@ public class DOMInspector implements INodeInspector, IElementInspector
Element parentElement = (Element)tgtParent;
Element newElement = null;
+ final Document ownerDocument = parentElement.getOwnerDocument();
if (srcElement instanceof Element)
{
Element source = (Element)srcElement;
- if (!name.equals(source.getNodeName()))
+ if (!name.equals(source.getLocalName()))
{
- throw new CopyElementException(JET2Messages.DOMInspector_InconsistentElementName);
+ newElement = ownerDocument.createElement(name);
+ NodeList childNodes = source.getChildNodes();
+ for(int i = 0; i < childNodes.getLength(); i++) {
+ newElement.appendChild(ownerDocument.importNode(childNodes.item(i), true));
+ }
+ NamedNodeMap attributes = source.getAttributes();
+ for(int i = 0; i < attributes.getLength(); i++) {
+ newElement.setAttributeNodeNS((Attr)ownerDocument.importNode(attributes.item(i), false));
+ }
+ parentElement.appendChild(newElement);
+
+// throw new CopyElementException(JET2Messages.DOMInspector_InconsistentElementName);
+ } else {
+ newElement = (Element)ownerDocument.importNode(source, recursive);
+ parentElement.appendChild(newElement);
}
- newElement = (Element)parentElement.getOwnerDocument().importNode(source, recursive);
- parentElement.appendChild(newElement);
}
+ else if(srcElement instanceof Document) {
+ newElement = ownerDocument.createElement(name);
+ final NodeList childNodes = ((Document)srcElement).getChildNodes();
+ for(int i = 0; i < childNodes.getLength(); i++) {
+ newElement.appendChild(ownerDocument.importNode(childNodes.item(i), true));
+ }
+ parentElement.appendChild(newElement);
+ }
else
{
throw new CopyElementException(JET2Messages.DOMInspector_NotDOMObject);
@@ -381,7 +406,14 @@ public class DOMInspector implements INodeInspector, IElementInspector
public boolean testExpandedName(Object node, ExpandedName testName)
{
- return testName.equals(expandedNameOf(node));
+ boolean match = testName.equals(expandedNameOf(node));
+ if(!match) {
+ String localName = ((Node)node).getLocalName();
+ if(localName != null) {
+ match = testName.equals(new ExpandedName(localName));
+ }
+ }
+ return match;
}
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/EObjectInspector.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/EObjectInspector.java
index 9de85df..214fbd7 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/EObjectInspector.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/inspectors/EObjectInspector.java
@@ -17,7 +17,6 @@ package org.eclipse.jet.internal.xpath.inspectors;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -68,6 +67,96 @@ import org.eclipse.jet.xpath.inspector.SimpleElementRequiresValueException;
public class EObjectInspector implements IElementInspector, INodeInspectorExtension1
{
+ static final class ElementNameTester implements IXMLElementTester
+ {
+
+ private final ExpandedName expandedName;
+ private final ExtendedMetaData exMD;
+
+ public ElementNameTester(ExtendedMetaData exMD, ExpandedName expandedName)
+ {
+ this.expandedName = expandedName;
+ this.exMD = exMD;
+ }
+
+ public boolean matches(Object child)
+ {
+ if (child instanceof EObject)
+ {
+ final EObject eChild = (EObject)child;
+ final EReference containmentFeature = eChild.eContainmentFeature();
+ // Note, the final condition on class names prevents subclasses of the
+ // declared feature type from being accepted. This is necessary to handle
+ // matches against substitution group head elements, which must only return
+ // elements of the head type, not sub-types
+ if (expandedName.getLocalPart().equals(exMD.getName(containmentFeature)) &&
+ (expandedName.getNamespaceURI() == null
+ || expandedName.getNamespaceURI().equals(exMD.getNamespace(containmentFeature))) &&
+ (eChild instanceof AnyType || eChild.eClass().getName().equals(containmentFeature.getEType().getName())))
+ {
+ return true;
+ }
+ else {
+ // See whether this is a substitution element containmentFeature
+ final String childNS = exMD.getNamespace(eChild.eClass());
+ // If so, the child element's NS has a feature whose name is the test feature
+ final EStructuralFeature testFeature = exMD.getElement(childNS, expandedName.getLocalPart());
+ // and whose affiliation is the containment feature
+ final EStructuralFeature affiliation = testFeature != null ? exMD.getAffiliation(testFeature) : null;
+ if(affiliation != null
+ && affiliation.getName().equals(containmentFeature.getName())
+ && testFeature.getEType().equals(eChild.eClass())) {
+ return true;
+ }
+ }
+ }
+ else if (child instanceof EMFEAttrAsElementWrapper)
+ {
+ final EMFEAttrAsElementWrapper wrapper = (EMFEAttrAsElementWrapper)child;
+ final EStructuralFeature feature = wrapper.getFeature();
+ if (expandedName.getLocalPart().equals(exMD.getName(feature)) &&
+ (expandedName.getNamespaceURI() == null || expandedName.getNamespaceURI().equals(exMD.getNamespace(feature))))
+ {
+ return true;
+ }
+
+ }
+ return false;
+ }
+
+ }
+
+
+ static class SchemaElementTester implements IXMLElementTester
+ {
+ private final ExpandedName expandedName;
+ private final ExtendedMetaData exMD;
+
+ public SchemaElementTester(ExtendedMetaData exMD, ExpandedName expandedName)
+ {
+ this.expandedName = expandedName;
+ this.exMD = exMD;
+ }
+
+ public boolean matches(Object child)
+ {
+ if(child instanceof EObject) {
+ final EObject eChild = (EObject)child;
+ final EReference containmentFeature = eChild.eContainmentFeature();
+ final EStructuralFeature affiliation = exMD.getAffiliation(containmentFeature);
+ final EStructuralFeature testFeature = affiliation != null ? affiliation : containmentFeature;
+ if(expandedName.getLocalPart().equals(exMD.getName(testFeature))
+ && (expandedName.getNamespaceURI() == null
+ || expandedName.getNamespaceURI().equals(exMD.getNamespace(testFeature)))) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ }
+
+
private static final String SCHEMA_ELEMENT_PREFIX = "_schema-element_"; //$NON-NLS-1$
private static final String EMPTY_STRING = ""; //$NON-NLS-1$
@@ -730,7 +819,7 @@ public class EObjectInspector implements IElementInspector, INodeInspectorExtens
if (isXMLNode(eClass))
{
- return getXMLNamedElement(eObject, eClass, nameTestExpandedName);
+ return getXMLNamedElement2(eObject, eClass, nameTestExpandedName);
}
else
@@ -769,132 +858,47 @@ public class EObjectInspector implements IElementInspector, INodeInspectorExtens
}
}
- private Object[] getXMLNamedElement(EObject eObject, EClass eClass, ExpandedName nameTestExpandedName)
+ interface IXMLElementTester {
+ public abstract boolean matches(Object child);
+ }
+
+ private Object[] getXMLNamedElement2(EObject eObject, EClass eClass, ExpandedName expandedName)
{
- final ExtendedMetaData exMD = getExtendedMetaData(eObject);
-// exMD.get
- final String testNsURI = nameTestExpandedName.getNamespaceURI();
- boolean isSchemaElementTest = false;
- String testLocalPart = nameTestExpandedName.getLocalPart();
- if(testLocalPart.startsWith(SCHEMA_ELEMENT_PREFIX)) {
- testLocalPart = testLocalPart.substring(SCHEMA_ELEMENT_PREFIX.length());
- isSchemaElementTest = true;
- }
+ final Object[] children = getChildren(eObject);
+ final List result = new ArrayList(children.length);
- final String[] namespacesToTry = testNsURI != null ? new String[] {testNsURI} : getApplicableNamespaces(exMD, eObject,eClass);
- EStructuralFeature feature = null;
- for (int i = 0; i < namespacesToTry.length && feature == null; i++)
- {
- feature = exMD.getElement(eClass, namespacesToTry[i], testLocalPart);
- }
-
- if(feature == null && namespacesToTry.length == 1 && namespacesToTry[0] == null) {
- // if the document has not namespace, getElement doesn't find elements registered on document root
- // try to find it ourselves...
- EObject rootContainer = EcoreUtil.getRootContainer(eObject);
- feature = exMD.getElement(rootContainer.eClass(), null, testLocalPart);
- }
- if(feature == null) {
- // didn't find a direct feature, see if there is a 'demand' feature...
- feature = exMD.demandFeature(testNsURI, testLocalPart, true);
- }
+ final IXMLElementTester test = getElementTester(eObject, expandedName);
- if(feature != null) {
- EStructuralFeature affiliation = exMD.getAffiliation(eClass, feature);
- final int affiliationKind = exMD.getFeatureKind(affiliation);
- if(affiliationKind == ExtendedMetaData.ELEMENT_WILDCARD_FEATURE) {
- FeatureMap fm = (FeatureMap)eObject.eGet(affiliation);
- return fm.list(feature).toArray();
- } else {
- Object[] values = getFeatureValue(eObject, affiliation);
- // Can get here in the following cases:
- // 1) testLocalPart(feature) is the head of a substitution group
- // In this case, affiliation == feature, but values includes all group members.
- // Unless isSchemaElementTest == true, must filter for actual instances of feature.getEType()
- // 2) testLocalPart(feature) is a substitution group member (not the head)
- // In this case, affiliation is the head and values includes all group members.
- // Must filter for actual instances of feature.getEType()
- // 3) testLocalPart(feature) is a local or global element not associated with a feature map
- // In this case, affiliation == feature and values contains only feature.getEType() instances.
- // 4) testLocalPart(feature) is a global type and
- // So, unless isSchemaElementTest is true, we must filter...
- if(!isSchemaElementTest) {
- // Filter the result to those that match the declared type of the feature
- // This is necessary because as of EMF 2.4, substitution groups map to a single
- // e-feature without a feature map
- List newValues = new ArrayList(Arrays.asList(values));
- for (Iterator i = newValues.iterator(); i.hasNext();)
- {
- Object candidate = i.next();
- // candidate may be an instance of AnyType, which indicates
- // an element for which no schema information was available. In such
- // cases, there is no possibility of substitution, and there is no
- // need to filter the element
- if(candidate instanceof EObject
- && !(candidate instanceof AnyType)
- && ((EObject)candidate).eClass() != feature.getEType()) {
- i.remove();
- }
- }
- values = newValues.toArray();
- }
- return values;
+ for(int i = 0; i < children.length; i++) {
+ Object child = children[i];
+ if(test.matches(child)) {
+ result.add(child);
}
- } else {
- return new Object[0];
- }
- }
-
- private String[] getApplicableNamespaces(ExtendedMetaData exMD, EObject eObject, EClass eClass)
- {
- EObject rootContainer = EcoreUtil.getRootContainer(eObject);
- EReference prefixMapFeature = exMD.getXMLNSPrefixMapFeature(rootContainer.eClass());
- if(prefixMapFeature != null) {
- EcoreEMap map = (EcoreEMap)rootContainer.eGet(prefixMapFeature);
- String[] nsArray = (String[])map.values().toArray(new String[map.values().size()+1]);
- nsArray[nsArray.length - 1] = null;
- return nsArray;
- } else {
- return new String[] {null};
}
+ return result.toArray();
}
/**
+ * Factory method for an {@link IXMLElementTester}
* @param eObject
- * @param feature
+ * @param expandedName
* @return
*/
- private Object[] getFeatureValue(EObject eObject, EStructuralFeature feature)
+ private IXMLElementTester getElementTester(EObject eObject, ExpandedName expandedName)
{
- Object objResult = eObject.eGet(feature);
- if(objResult == null) {
- return new Object[0];
- }
- if(feature instanceof EReference) {
- if(feature.isMany()) {
- return ((List)objResult).toArray();
- } else {
- return new Object[] {objResult};
- }
+ final ExtendedMetaData exMD = getExtendedMetaData(eObject);
+ final String localPart = expandedName.getLocalPart();
+ final IXMLElementTester test;
+ if(localPart.startsWith(SCHEMA_ELEMENT_PREFIX)) {
+ test = new SchemaElementTester(exMD,
+ new ExpandedName(expandedName.getNamespaceURI(), localPart.substring(SCHEMA_ELEMENT_PREFIX.length())));
} else {
- // its an EAttribute, wrap...
- final Setting setting = ((InternalEObject)eObject).eSetting(feature);
- if(feature.isMany())
- {
- Object[] result = new Object[((List)objResult).size()];
- for(int j = 0; j < result.length; j++ ) {
- result[j] = new EMFEAttrAsElementWrapper(setting, j);
- }
- return result;
- }
- else
- {
- return new Object[] {new EMFEAttrAsElementWrapper(setting)};
- }
+ test = new ElementNameTester(exMD, expandedName);
}
+ return test;
}
-
+
/**
* @param eClass
* @param featureURI
@@ -1140,7 +1144,8 @@ public class EObjectInspector implements IElementInspector, INodeInspectorExtens
Object newElement = null;
- if (srcElement instanceof EObject)
+ final NodeKind nodeKind = getNodeKind(srcElement);
+ if (srcElement instanceof EObject && nodeKind == NodeKind.ELEMENT)
{
EObject srcEObject = (EObject)srcElement;
if (recursive)
@@ -1169,6 +1174,35 @@ public class EObjectInspector implements IElementInspector, INodeInspectorExtens
addElementToParent(eObject, feature, newElement, null);
}
+ else if( srcElement instanceof EObject && nodeKind == NodeKind.ROOT)
+ {
+ try
+ {
+ newElement = addElement(eObject, new ExpandedName(name), null);
+ if(recursive) {
+ final Object[] children = getChildren(srcElement);
+ Object root = null;
+ for (int i = 0; i < children.length; i++)
+ {
+ if(children[i] instanceof EObject && getNodeKind(children[i]) == NodeKind.ELEMENT) {
+ root = children[i];
+ break;
+ }
+ }
+ if(root != null) {
+ copyElement(newElement, root, expandedNameOf(root).getLocalPart(), true);
+ }
+ }
+ }
+ catch (SimpleElementRequiresValueException e)
+ {
+ throw new CopyElementException(e);
+ }
+ catch (InvalidChildException e)
+ {
+ throw new CopyElementException(e);
+ }
+ }
else if (srcElement instanceof EMFEAttrAsElementWrapper)
{
EMFEAttrAsElementWrapper wrapper = (EMFEAttrAsElementWrapper)srcElement;
diff --git a/tests/org.eclipse.jet.tests/.settings/org.eclipse.jdt.ui.prefs b/tests/org.eclipse.jet.tests/.settings/org.eclipse.jdt.ui.prefs
new file mode 100644
index 0000000..2e01db6
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/.settings/org.eclipse.jdt.ui.prefs
@@ -0,0 +1,4 @@
+#Thu Sep 25 16:24:39 EDT 2008
+eclipse.preferences.version=1
+org.eclipse.jdt.ui.javadoc=false
+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 * 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 * /\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/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/AbstractCopyElementTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/AbstractCopyElementTests.java
new file mode 100644
index 0000000..1e95dd0
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/AbstractCopyElementTests.java
@@ -0,0 +1,82 @@
+/*******************************************************************************
+ * Copyright (c) 2008 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.tests.xpath.inspectors;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.eclipse.jet.runtime.model.IModelLoader;
+import org.eclipse.jet.xpath.XPath;
+import org.eclipse.jet.xpath.XPathException;
+import org.eclipse.jet.xpath.XPathExpression;
+import org.eclipse.jet.xpath.XPathFactory;
+import org.eclipse.jet.xpath.inspector.CopyElementException;
+import org.eclipse.jet.xpath.inspector.IElementInspector;
+import org.eclipse.jet.xpath.inspector.InspectorManager;
+import org.eclipse.jet.xpath.inspector.INodeInspector.NodeKind;
+
+public abstract class AbstractCopyElementTests extends TestCase {
+
+ private Object loadDocument(String documentPath) throws IOException {
+ IModelLoader loader = getModelLoader();
+ return loader.loadFromString(documentPath, getModelKind());
+ }
+
+ protected String getModelKind() {
+ return "xml";
+ }
+
+ protected abstract IModelLoader getModelLoader();
+
+ public void testCopyDocument() throws IOException, CopyElementException, XPathException {
+ String docAContent = "<rootA><element value=\"A\"/></rootA>";
+ String docBContent = "<rootB><element value=\"B\"/></rootB>";
+ Object docA = loadDocument(docAContent);
+ Object docB = loadDocument(docBContent);
+
+ final InspectorManager im = InspectorManager.getInstance();
+
+ final Object rootElementB = getRootElement(docB, im);
+ final IElementInspector inspector = (IElementInspector) im.getInspector(rootElementB);
+
+ final Object copiedDocA = inspector.copyElement(rootElementB, docA, "copied", true);
+
+ final XPath XPath = XPathFactory.newInstance().newXPath(null);
+ final XPathExpression xRootA_element_value = XPath.compile("rootA/element/@value");
+ final XPathExpression xRootB_element_value = XPath.compile("rootB/element/@value");
+ final XPathExpression xRootB_RootA_element_value = XPath.compile("rootB/copied/rootA/element/@value");
+
+
+ assertEquals("Original A", "A", xRootA_element_value.evaluateAsString(docA));
+ assertEquals("Original B", "B", xRootB_element_value.evaluateAsString(docB));
+ assertEquals("Copied A", "A", xRootA_element_value.evaluateAsString(copiedDocA));
+ assertEquals("Copied A, within B", "A", xRootB_RootA_element_value.evaluateAsString(docB));
+
+
+
+ }
+
+ private Object getRootElement(Object docA, final InspectorManager im) {
+ final Object[] children = im.getInspector(docA).getChildren(docA);
+
+ for (int i = 0; i < children.length; i++) {
+ if(im.getInspector(children[i]).getNodeKind(children[i]) == NodeKind.ELEMENT) {
+ return children[i];
+ }
+ }
+ return null;
+ }
+
+
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/AllTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/AllTests.java
index 7cb6e9c..754efb3 100644
--- a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/AllTests.java
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/AllTests.java
@@ -1,3 +1,15 @@
+/*******************************************************************************
+ * Copyright (c) 2008 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.tests.xpath.inspectors;
import junit.framework.Test;
@@ -9,6 +21,8 @@ public class AllTests {
TestSuite suite = new TestSuite(
"Test for org.eclipse.jet.tests.xpath.inspectors");
//$JUnit-BEGIN$
+ suite.addTestSuite(XMLDOMCopyElementTests.class);
+ suite.addTestSuite(EMFXMLCopyELementTests.class);
suite.addTestSuite(EMFXML24SubstitutionGroupTests.class);
suite.addTestSuite(LegacyEMFXMLSubstitutionGroupTests.class);
//$JUnit-END$
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/EMFXMLCopyELementTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/EMFXMLCopyELementTests.java
new file mode 100644
index 0000000..968f03a
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/EMFXMLCopyELementTests.java
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * Copyright (c) 2008 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.tests.xpath.inspectors;
+
+import org.eclipse.jet.internal.runtime.model.EMFXMLModelLoader;
+import org.eclipse.jet.runtime.model.IModelLoader;
+
+/**
+ * Perform CopyElementTests using the EMF XML Model loader
+ *
+ */
+public class EMFXMLCopyELementTests extends AbstractCopyElementTests {
+
+ protected IModelLoader getModelLoader() {
+ return new EMFXMLModelLoader();
+ }
+
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/XMLDOMCopyElementTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/XMLDOMCopyElementTests.java
new file mode 100644
index 0000000..674acd3
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/inspectors/XMLDOMCopyElementTests.java
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * Copyright (c) 2008 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.tests.xpath.inspectors;
+
+import org.eclipse.jet.internal.runtime.model.XMLDOMLoader;
+import org.eclipse.jet.runtime.model.IModelLoader;
+
+/**
+ * Perform CopyElementTests using the XML DOM Model loader
+ *
+ */
+public class XMLDOMCopyElementTests extends AbstractCopyElementTests {
+
+ protected IModelLoader getModelLoader() {
+ return new XMLDOMLoader();
+ }
+
+}

Back to the top