Anonymous avatar Anonymous committed 904f6bf

XW-535
- external-ref tag's content cannot be read occasionally by some parser

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/xwork_1-2@1539 e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

src/java/com/opensymphony/xwork/config/providers/XmlConfigurationProvider.java

                 //If the external ref is not declared explicitly, we can introspect the
                 //reference type using it's name and try resolving the reference using it's class type
                 if (refElement.getChildNodes().getLength() > 0) {
-                    refValue = refElement.getChildNodes().item(0).getNodeValue();
+                    refValue = XmlHelper.getContent(refElement);
                 }
 
                 requiredTemp = refElement.getAttribute("required");
 
                         String paramName = config.getDefaultResultParam();
                         if (paramName != null) {
-                            StringBuffer paramValue = new StringBuffer();
-                            for (int j=0; j<resultElement.getChildNodes().getLength(); j++) {
-                                if (resultElement.getChildNodes().item(j).getNodeType() == Node.TEXT_NODE) {
-                                    String val = resultElement.getChildNodes().item(j).getNodeValue();
-                                    if (val != null) {
-                                        paramValue.append(val);
-                                    }
-                                }
-                            }
-                            String val = paramValue.toString().trim();
+                            String val = XmlHelper.getContent(resultElement);
                             if (val.length() > 0) {
                                 resultParams.put(paramName, val);
                             }

src/java/com/opensymphony/xwork/config/providers/XmlHelper.java

  * XML utilities.
  *
  * @author Mike
+ * @author tmjee
  */
 public class XmlHelper {
 
+    /**
+     * This method will find all the parameters under this <code>paramsElement</code> and return them as
+     * Map<String, String>. For example,
+     * <pre>
+     *   <result ... >
+     *      <param name="param1">value1</param>
+     *      <param name="param2">value2</param>
+     *      <param name="param3">value3</param>
+     *   </result>
+     * </pre>
+     * will returns a Map<String, String> with the following key, value pairs :-
+     * <ul>
+     *  <li>param1 - value1</li>
+     *  <li>param2 - value2</li>
+     *  <li>param3 - value3</li>
+     * </ul>
+     *
+     * @param paramsElement
+     * @return
+     */
     public static Map getParams(Element paramsElement) {
         LinkedHashMap params = new LinkedHashMap();
 
         if (paramsElement == null) {
             return params;
         }
-
-        
         
         NodeList childNodes = paramsElement.getChildNodes();
         for (int i = 0; i < childNodes.getLength(); i++) {
                 Element paramElement = (Element) childNode;
                 String paramName = paramElement.getAttribute("name");
                 
-                StringBuffer paramValue = new StringBuffer();
-                for (int j=0; j <paramElement.getChildNodes().getLength(); j++) {
-                	if (paramElement.getChildNodes().item(j) != null && 
-                			paramElement.getChildNodes().item(j).getNodeType() == Node.TEXT_NODE) {
-                		String val = paramElement.getChildNodes().item(j).getNodeValue();
-                		if (val != null) {
-                			paramValue .append(val.trim());
-                		} 
-                	}
-                }
-                String val = paramValue.toString().trim();
+                String val = getContent(paramElement);
                 if (val.length() > 0) {
                 	params.put(paramName, val);
                 }
         }
         return params;
     }
+
+
+    /**
+     * This method will return the content of this particular <code>element</code>.
+     * For example,
+     *
+     * <pre>
+     *    <result>something_1</result>
+     * </pre>
+     * When the {@link org.w3c.dom.Element} <code>&lt;result&gt;</code> is passed in as
+     * argument (<code>element</code> to this method, it returns the content of it,
+     * namely, <code>something_1</code> in the example above.
+     * 
+     * @return
+     */
+    public static String getContent(Element element) {
+        StringBuffer paramValue = new StringBuffer();
+        NodeList childNodes = element.getChildNodes();
+        for (int j=0; j <childNodes.getLength(); j++) {
+            Node currentNode = childNodes.item(j);
+            if (currentNode != null &&
+                    currentNode.getNodeType() == Node.TEXT_NODE) {
+                String val = currentNode.getNodeValue();
+                if (val != null) {
+                    paramValue .append(val.trim());
+                }
+            }
+        }
+        String val = paramValue.toString().trim();
+        return val;
+    }
 }

src/test/com/opensymphony/xwork/config/providers/XmlHelperTest.java

+package com.opensymphony.xwork.config.providers;
+
+import com.opensymphony.xwork.XWorkTestCase;
+import org.easymock.MockControl;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Node;
+
+import java.util.Map;
+
+/**
+ * @author tmjee
+ * @version $Date$ $Id$
+ */
+public class XmlHelperTest extends XWorkTestCase {
+
+    public void testGetContent1() throws Exception {
+        // set up Node
+        MockControl nodeControl = MockControl.createControl(Node.class);
+        Node mockNode = (Node) nodeControl.getMock();
+
+        nodeControl.expectAndDefaultReturn(mockNode.getNodeValue(), "testing testing 123");
+        nodeControl.expectAndDefaultReturn(mockNode.getNodeType(), Node.TEXT_NODE);
+
+
+        // set up NodeList
+        MockControl nodeListControl = MockControl.createControl(NodeList.class);
+        NodeList mockNodeList = (NodeList) nodeListControl.getMock();
+
+        nodeListControl.expectAndDefaultReturn(mockNodeList.getLength(), 1);
+        nodeListControl.expectAndDefaultReturn(mockNodeList.item(0), mockNode);
+
+
+        // set up Element
+        MockControl elementControl = MockControl.createControl(Element.class);
+        Element mockElement = (Element) elementControl.getMock();
+
+        elementControl.expectAndDefaultReturn(mockElement.getChildNodes(), mockNodeList);
+
+        nodeControl.replay();
+        nodeListControl.replay();
+        elementControl.replay();
+
+        String result = XmlHelper.getContent(mockElement);
+
+        nodeControl.verify();
+        nodeListControl.verify();
+        elementControl.verify();
+
+        assertEquals(result, "testing testing 123");
+    }
+
+
+    public void testGetContent2() throws Exception {
+        // set up Node
+        MockControl nodeControl1 = MockControl.createControl(Node.class);
+        Node mockNode1 = (Node) nodeControl1.getMock();
+
+        nodeControl1.expectAndDefaultReturn(mockNode1.getNodeValue(), "testing testing 123");
+        nodeControl1.expectAndDefaultReturn(mockNode1.getNodeType(), Node.TEXT_NODE);
+
+        MockControl nodeControl2 = MockControl.createControl(Node.class);
+        Node mockNode2 = (Node) nodeControl2.getMock();
+
+        nodeControl2.expectAndDefaultReturn(mockNode2.getNodeValue(), "comment 1");
+        nodeControl2.expectAndDefaultReturn(mockNode2.getNodeType(), Node.COMMENT_NODE);
+
+        MockControl nodeControl3 = MockControl.createControl(Node.class);
+        Node mockNode3 = (Node) nodeControl3.getMock();
+
+        nodeControl3.expectAndDefaultReturn(mockNode3.getNodeValue(), " tmjee ");
+        nodeControl3.expectAndDefaultReturn(mockNode3.getNodeType(), Node.TEXT_NODE);
+
+        MockControl nodeControl4 = MockControl.createControl(Node.class);
+        Node mockNode4 = (Node) nodeControl4.getMock();
+
+        nodeControl4.expectAndDefaultReturn(mockNode4.getNodeValue(), " phil ");
+        nodeControl4.expectAndDefaultReturn(mockNode4.getNodeType(), Node.TEXT_NODE);
+
+        MockControl nodeControl5 = MockControl.createControl(Node.class);
+        Node mockNode5 = (Node) nodeControl5.getMock();
+
+        nodeControl5.expectAndDefaultReturn(mockNode5.getNodeValue(), "comment 2");
+        nodeControl5.expectAndDefaultReturn(mockNode5.getNodeType(), Node.COMMENT_NODE);
+
+        MockControl nodeControl6 = MockControl.createControl(Node.class);
+        Node mockNode6 = (Node) nodeControl6.getMock();
+
+        nodeControl6.expectAndDefaultReturn(mockNode6.getNodeValue(), "comment 3");
+        nodeControl6.expectAndDefaultReturn(mockNode6.getNodeType(), Node.COMMENT_NODE);
+
+
+        // set up NodeList
+        MockControl nodeListControl = MockControl.createControl(NodeList.class);
+        NodeList mockNodeList = (NodeList) nodeListControl.getMock();
+
+        nodeListControl.expectAndDefaultReturn(mockNodeList.getLength(), 6);
+        mockNodeList.item(0);
+        nodeListControl.setReturnValue(mockNode1);
+        mockNodeList.item(1);
+        nodeListControl.setReturnValue(mockNode2);
+        mockNodeList.item(2);
+        nodeListControl.setDefaultReturnValue(mockNode3);
+        mockNodeList.item(3);
+        nodeListControl.setReturnValue(mockNode4);
+        mockNodeList.item(4);
+        nodeListControl.setReturnValue(mockNode5);
+        mockNodeList.item(5);
+        nodeListControl.setReturnValue(mockNode6);
+
+
+        // set up Element
+        MockControl elementControl = MockControl.createControl(Element.class);
+        Element mockElement = (Element) elementControl.getMock();
+
+        elementControl.expectAndDefaultReturn(mockElement.getChildNodes(), mockNodeList);
+
+        nodeControl1.replay();
+        nodeControl2.replay();
+        nodeControl3.replay();
+        nodeControl4.replay();
+        nodeControl5.replay();
+        nodeControl6.replay();
+        nodeListControl.replay();
+        elementControl.replay();
+
+        String result = XmlHelper.getContent(mockElement);
+
+        nodeControl1.verify();
+        nodeControl2.verify();
+        nodeControl3.verify();
+        nodeControl4.verify();
+        nodeControl5.verify();
+        nodeControl6.verify();
+        nodeListControl.verify();
+        elementControl.verify();
+
+        assertEquals(result, "testing testing 123tmjeephil");
+    }
+
+
+
+    public void testGetParams() throws Exception {
+        // <param name="param1">value1</param>
+            MockControl nodeControl1 = MockControl.createControl(Node.class);
+            Node mockNode1 = (Node) nodeControl1.getMock();
+
+            nodeControl1.expectAndDefaultReturn(mockNode1.getNodeValue(), "value1");
+            nodeControl1.expectAndDefaultReturn(mockNode1.getNodeType(), Node.TEXT_NODE);
+
+
+            MockControl nodeListControl1 = MockControl.createControl(NodeList.class);
+            NodeList mockNodeList1 = (NodeList) nodeListControl1.getMock();
+
+            nodeListControl1.expectAndDefaultReturn(mockNodeList1.getLength(), 1);
+            nodeListControl1.expectAndDefaultReturn(mockNodeList1.item(0), mockNode1);
+
+            MockControl paramControl1 = MockControl.createControl(Element.class);
+            Element mockParamElement1 = (Element) paramControl1.getMock();
+            mockParamElement1.getNodeName();
+            paramControl1.setReturnValue("param");
+
+            mockParamElement1.getNodeType();
+            paramControl1.setReturnValue(Node.ELEMENT_NODE);
+
+            mockParamElement1.getAttribute("name");
+            paramControl1.setReturnValue("param1");
+
+            mockParamElement1.getChildNodes();
+            paramControl1.setReturnValue(mockNodeList1);
+
+            nodeControl1.replay();
+            nodeListControl1.replay();
+            paramControl1.replay();
+
+        // <param name="param2">value2</param>
+            MockControl nodeControl2 = MockControl.createControl(Node.class);
+            Node mockNode2 = (Node) nodeControl2.getMock();
+
+            nodeControl2.expectAndDefaultReturn(mockNode2.getNodeValue(), "value2");
+            nodeControl2.expectAndDefaultReturn(mockNode2.getNodeType(), Node.TEXT_NODE);
+
+
+            MockControl nodeListControl2 = MockControl.createControl(NodeList.class);
+            NodeList mockNodeList2 = (NodeList) nodeListControl2.getMock();
+
+            nodeListControl2.expectAndDefaultReturn(mockNodeList2.getLength(), 1);
+            nodeListControl2.expectAndDefaultReturn(mockNodeList2.item(0), mockNode2);
+
+            MockControl paramControl2 = MockControl.createControl(Element.class);
+            Element mockParamElement2 = (Element) paramControl2.getMock();
+            mockParamElement2.getNodeName();
+            paramControl2.setReturnValue("param");
+
+            mockParamElement2.getNodeType();
+            paramControl2.setReturnValue(Node.ELEMENT_NODE);
+
+            mockParamElement2.getAttribute("name");
+            paramControl2.setReturnValue("param2");
+
+            mockParamElement2.getChildNodes();
+            paramControl2.setReturnValue(mockNodeList2);
+
+            nodeControl2.replay();
+            nodeListControl2.replay();
+            paramControl2.replay();
+
+
+        // <some_element>
+        //   ...
+        // </some_element>
+        MockControl elementNodeListControl = MockControl.createControl(NodeList.class);
+        NodeList mockElementNodeList = (NodeList) elementNodeListControl.getMock();
+
+        elementNodeListControl.expectAndDefaultReturn(mockElementNodeList.getLength(), 2);
+        mockElementNodeList.item(0);
+        elementNodeListControl.setReturnValue(mockParamElement2);
+        mockElementNodeList.item(1);
+        elementNodeListControl.setReturnValue(mockParamElement1);
+
+        MockControl elementControl = MockControl.createControl(Element.class);
+        Element element = (Element) elementControl.getMock();
+
+        elementControl.expectAndDefaultReturn(element.getChildNodes(), mockElementNodeList);
+
+
+        elementNodeListControl.replay();
+        elementControl.replay();
+
+
+
+        Map params = XmlHelper.getParams(element);
+
+        nodeControl1.verify();
+            nodeListControl1.verify();
+            paramControl1.verify();
+
+
+        nodeControl2.verify();
+            nodeListControl2.verify();
+            paramControl2.verify();
+
+
+        elementNodeListControl.verify();
+        elementControl.verify();
+
+
+        assertNotNull(params);
+        assertEquals(params.size(), 2);
+        assertEquals(params.get("param1"), "value1");
+        assertEquals(params.get("param2"), "value2");
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.