Author: yradtsevich
Date: 2010-10-05 08:13:48 -0400 (Tue, 05 Oct 2010)
New Revision: 25487
Added:
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/7209/
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/7209/getNodeByXPath.html
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/SourceDomUtilTest.java
Modified:
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/JsfAllTests.java
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DndUtil.java
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/VpeDnD.java
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/SourceDomUtil.java
trunk/vpe/tests/org.jboss.tools.vpe.ui.test/src/org/jboss/tools/vpe/ui/test/ComponentContentTest.java
Log:
https://jira.jboss.org/browse/JBIDE-7209 :
DnD does not work for files with XHTML namespace
- calls to org.eclipse.wst.xml.xpath.core.util.XSLTXPathHelper.selectSingleNode which
works incorrectly are replaced by new method
org.jboss.tools.vpe.editor.util.SourceDomUtil.getNodeByXPath
Added:
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/7209/getNodeByXPath.html
===================================================================
---
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/7209/getNodeByXPath.html
(rev 0)
+++
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/resources/jsfTest/WebContent/pages/JBIDE/7209/getNodeByXPath.html 2010-10-05
12:13:48 UTC (rev 25487)
@@ -0,0 +1,28 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html
xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<style type="text/css">
+table {
+ background-color: black;
+ table-layout: fixed;
+}
+td {
+ background-color: white;
+ width: 100px;
+ height: 100px;
+}
+</style>
+</head>
+<body>
+ <table>
+ <tr>
+ <td id="id1" onclick="f()">
+ <input type="button" value="Button" />
+ </td>
+
+ <td id="id2">
+ </td>
+ </tr>
+ </table>
+</body>
+</html>
Modified:
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/JsfAllTests.java
===================================================================
---
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/JsfAllTests.java 2010-10-05
12:10:20 UTC (rev 25486)
+++
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/JsfAllTests.java 2010-10-05
12:13:48 UTC (rev 25487)
@@ -93,6 +93,7 @@
import org.jboss.tools.jsf.vpe.jsf.test.jbide.RenderFacetAndInsertChildrenTest;
import org.jboss.tools.jsf.vpe.jsf.test.jbide.SelectAllAndCut_JBIDE4853;
import org.jboss.tools.jsf.vpe.jsf.test.jbide.SelectWholeElement_JBIDE4713;
+import org.jboss.tools.jsf.vpe.jsf.test.jbide.SourceDomUtilTest;
import org.jboss.tools.jsf.vpe.jsf.test.jbide.TaglibXMLUnformatedDTD_JBIDE5642;
import org.jboss.tools.jsf.vpe.jsf.test.jbide.TestContextPathResolution;
import org.jboss.tools.jsf.vpe.jsf.test.jbide.TestFViewLocaleAttribute_JBIDE5218;
@@ -102,6 +103,7 @@
import org.jboss.tools.jsf.vpe.jsf.test.jbide.VPERefreshTest;
import org.jboss.tools.jsf.vpe.jsf.test.jbide.VisualRefreshComment_JBIDE6067;
import org.jboss.tools.jsf.vpe.jsf.test.jbide.VpeI18nTest_JBIDE4887;
+import org.jboss.tools.jsf.vpe.jsf.test.jbide.XulRunnerVpeUtilsTest;
import org.jboss.tools.vpe.ui.test.VpeTestSetup;
/**
@@ -135,6 +137,8 @@
/*
* Other tests
*/
+ suite.addTestSuite(SourceDomUtilTest.class);
+ suite.addTestSuite(XulRunnerVpeUtilsTest.class);
suite.addTestSuite(JSF2ValidatorTest.class);
suite.addTestSuite(DocBookTemplatesTest.class);
// suite.addTestSuite(DnD_JBIDE5042_JBIDE6229_Test.class);
Added:
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/SourceDomUtilTest.java
===================================================================
---
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/SourceDomUtilTest.java
(rev 0)
+++
trunk/jsf/tests/org.jboss.tools.jsf.vpe.jsf.test/src/org/jboss/tools/jsf/vpe/jsf/test/jbide/SourceDomUtilTest.java 2010-10-05
12:13:48 UTC (rev 25487)
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.jsf.vpe.jsf.test.jbide;
+
+import java.text.MessageFormat;
+
+import org.jboss.tools.jsf.vpe.jsf.test.JsfAllTests;
+import org.jboss.tools.vpe.editor.VpeController;
+import org.jboss.tools.vpe.editor.util.SourceDomUtil;
+import org.jboss.tools.vpe.ui.test.VpeTest;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+
+/**
+ * Tests for {@link SourceDomUtil} class.
+ *
+ * @author Yahor Radtsevich (yradtsevich)
+ * @see <a
href="https://jira.jboss.org/browse/JBIDE-7209">JBIDE-7209&l...
+ */
+public class SourceDomUtilTest extends VpeTest {
+ private static final String TEST_FILE_PATH
+ = "JBIDE/7209/getNodeByXPath.html"; //$NON-NLS-1$
+ private static final String ID_1 = "id1"; //$NON-NLS-1$
+ private static final String ID_2 = "id2"; //$NON-NLS-1$
+ private static final String X_PATH_3 = "/html/body/table/tr/td[1]/@onclick";
//$NON-NLS-1$
+ private static final String VALUE_3 = "f()"; //$NON-NLS-1$
+
+ private VpeController vpeController;
+
+ public SourceDomUtilTest(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ vpeController = openInVpe(JsfAllTests.IMPORT_PROJECT_NAME,
+ TEST_FILE_PATH);
+ }
+
+ ////////////////////////////////////////////////////////////////////////////
+ // JUNIT TESTING METHODS
+ /**
+ * Test for {@link SourceDomUtil#getXPath(Node)} and
+ * {@link SourceDomUtil#getNodeByXPath(Document, String)}.
+ *
+ * @see <a
href="https://jira.jboss.org/browse/JBIDE-7209">JBIDE-7209&l...
+ */
+ public void testGetNodeByXPath() {
+ Document document = vpeController.getPageContext()
+ .getSourceBuilder().getSourceDocument();
+
+ Node node1 = document.getElementById(ID_1);
+ assertNotNull(node1);
+ String xPath1 = SourceDomUtil.getXPath(node1);
+ assertEquals(MessageFormat.format(
+ "getNodeByXPath is not inverse of getXPath for {0}", ID_1), //$NON-NLS-1$
+ node1, SourceDomUtil.getNodeByXPath(document, xPath1));
+
+ Node node2 = document.getElementById(ID_2);
+ assertNotNull(node2);
+ String xPath2 = SourceDomUtil.getXPath(node2);
+ assertEquals(MessageFormat.format(
+ "getNodeByXPath is not inverse of getXPath for {0}", ID_2), //$NON-NLS-1$
+ node2, SourceDomUtil.getNodeByXPath(document, xPath2));
+
+ Node node3 = SourceDomUtil.getNodeByXPath(document, X_PATH_3);
+ assertTrue(MessageFormat.format("{0} is not an attribute", X_PATH_3),
//$NON-NLS-1$
+ node3 instanceof Attr);
+ assertEquals(MessageFormat.format(
+ "Value of {0} is not equal to {1}", X_PATH_3, VALUE_3), //$NON-NLS-1$
+ VALUE_3, ((Attr)node3).getValue());
+ }
+}
Modified: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DndUtil.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DndUtil.java 2010-10-05
12:10:20 UTC (rev 25486)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/DndUtil.java 2010-10-05
12:13:48 UTC (rev 25487)
@@ -268,7 +268,8 @@
public static Node getNodeFromDragSession(VpePageContext pageContext) {
String xPath = DndUtil.getDragTransferDataAsString(VPE_XPATH_FLAVOR);
if (xPath != null) {
- return SourceDomUtil.getNodeByXPath(pageContext, xPath);
+ return SourceDomUtil.getNodeByXPath(
+ pageContext.getSourceBuilder().getSourceDocument(), xPath);
} else {
return null;
}
Modified: trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/VpeDnD.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/VpeDnD.java 2010-10-05
12:10:20 UTC (rev 25486)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/dnd/VpeDnD.java 2010-10-05
12:13:48 UTC (rev 25487)
@@ -42,6 +42,7 @@
import org.jboss.tools.vpe.editor.mozilla.MozillaEditor;
import org.jboss.tools.vpe.editor.mozilla.listener.MozillaDndListener;
import org.jboss.tools.vpe.editor.mozilla.listener.MozillaSelectionListener;
+import org.jboss.tools.vpe.editor.util.SourceDomUtil;
import org.jboss.tools.vpe.editor.util.VisualDomUtil;
import org.jboss.tools.vpe.editor.util.VpeDndUtil;
import org.jboss.tools.vpe.messages.VpeUIMessages;
@@ -488,7 +489,7 @@
iTransferable.setTransferData("text/html", nodeSourceData,
nodeSourceDataLength); //$NON-NLS-1$
iTransferable.setTransferData("text/unicode", nodeSourceData,
nodeSourceDataLength); //$NON-NLS-1$
- String xPath = XSLTXPathHelper.calculateXPathToNode(node);
+ String xPath = SourceDomUtil.getXPath(node);
nsISupportsString xPathData = createNsISupportsString(xPath);
iTransferable.setTransferData(DndUtil.VPE_XPATH_FLAVOR, xPathData, xPath.length() *
2);
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/SourceDomUtil.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/SourceDomUtil.java 2010-10-05
12:10:20 UTC (rev 25486)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/SourceDomUtil.java 2010-10-05
12:13:48 UTC (rev 25487)
@@ -13,15 +13,13 @@
import java.util.HashSet;
import java.util.Set;
-import javax.xml.transform.TransformerException;
-
import org.eclipse.wst.xml.xpath.core.util.XSLTXPathHelper;
-import org.jboss.tools.vpe.VpePlugin;
import org.jboss.tools.vpe.editor.context.VpePageContext;
import org.jboss.tools.vpe.editor.mapping.VpeDomMapping;
import org.jboss.tools.vpe.editor.mapping.VpeNodeMapping;
import org.jboss.tools.vpe.editor.proxy.VpeProxyUtil;
import org.jboss.tools.vpe.editor.template.VpeTemplateManager;
+import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -114,16 +112,73 @@
}
/**
- * Returns source node by its XPath.
+ * Returns a XPath expression given a DOM Node.
+ *
+ * @param node The DOM Node to create the XPath expression.
+ *
+ * @see XSLTXPathHelper#calculateXPathToNode(Node)
*/
- public static Node getNodeByXPath(VpePageContext pageContext, String xPath) {
- Node node = null;
+ public static String getXPath(Node node) {
+ return XSLTXPathHelper.calculateXPathToNode(node);
+ }
+
+ /**
+ * Inverse function for {@link #getXPath(Node)}.
+ *
+ * @param document ancestor document for xPath
+ * @param xPath XPath to a node in one of the following form:
+ * <code>
+ * <br> "/html/body/table/tr/td",
+ * <br> "/html/body/table/tr/td[1]",
+ * <br>&nbsp;"/html/body/table/tr/td[1]/(a)onclick".</code>
+ *
+ * @return node for the given {@code xPath}, or {@code null}
+ * if the node is not found.
+ */
+ public static Node getNodeByXPath(Document document, String xPath) {
+ Node currentNode = document;
try {
- node = XSLTXPathHelper.selectSingleNode(
- pageContext.getSourceBuilder().getSourceDocument(), xPath);
- } catch (TransformerException e) {
- VpePlugin.reportProblem(e);
+ String[] nodeNames = xPath.split("/"); //$NON-NLS-1$
+
+ // begin from 1 to skip the first element which is empty
+ for (int i = 1; i < nodeNames.length; i++) {
+ String nodeName = nodeNames[i];
+ if (nodeName.charAt(0) != '@') {
+ currentNode = currentNode.getFirstChild();
+ if (nodeName.charAt(nodeName.length() - 1) != ']') {
+ while (currentNode.getNodeType() != Node.ELEMENT_NODE
+ || !currentNode.getNodeName().equals(nodeName)) {
+ currentNode = currentNode.getNextSibling();
+ }
+ } else {
+ int openingBracketIndex = nodeName.lastIndexOf('[');
+ String stringPosition = nodeName.substring(
+ openingBracketIndex + 1,
+ nodeName.length() - 1);
+ nodeName = nodeName.substring(0, openingBracketIndex);
+
+ int position = Integer.parseInt(stringPosition);
+ int curPosition = 0;
+ while (true) {
+ if (currentNode.getNodeType() == Node.ELEMENT_NODE
+ && currentNode.getNodeName().equals(nodeName)) {
+ ++curPosition;
+ if (curPosition == position) {
+ break;
+ }
+ }
+ currentNode = currentNode.getNextSibling();
+ }
+ }
+ } else {
+ String attributeName = nodeName.substring(1, nodeName.length());
+ currentNode = currentNode.getAttributes().getNamedItem(attributeName);
+ }
+ }
+ } catch (Exception e) {
+ return null;
}
- return node;
+
+ return currentNode;
}
}
Modified:
trunk/vpe/tests/org.jboss.tools.vpe.ui.test/src/org/jboss/tools/vpe/ui/test/ComponentContentTest.java
===================================================================
---
trunk/vpe/tests/org.jboss.tools.vpe.ui.test/src/org/jboss/tools/vpe/ui/test/ComponentContentTest.java 2010-10-05
12:10:20 UTC (rev 25486)
+++
trunk/vpe/tests/org.jboss.tools.vpe.ui.test/src/org/jboss/tools/vpe/ui/test/ComponentContentTest.java 2010-10-05
12:13:48 UTC (rev 25487)
@@ -24,6 +24,7 @@
import org.jboss.tools.test.util.WorkbenchUtils;
import org.jboss.tools.vpe.editor.VpeController;
import org.jboss.tools.vpe.editor.mapping.VpeNodeMapping;
+import org.jboss.tools.vpe.editor.util.SourceDomUtil;
import org.mozilla.interfaces.nsIDOMElement;
import org.mozilla.interfaces.nsIDOMNode;
import org.w3c.dom.Document;
@@ -110,7 +111,7 @@
try{
compareElements(controller, xmlTestDocument, id, id);
} catch (DOMComparisonException e) {
- String xPathToNode = XSLTXPathHelper.calculateXPathToNode(e.getNode());
+ String xPathToNode = SourceDomUtil.getXPath(e.getNode());
String testFileName = xmlTestFile.getPath();
String message = e.getMessage();
fail(String.format("%s[%s]:\n%s", testFileName, xPathToNode, message));
//$NON-NLS-1$