1 50 51 package org.openlaszlo.iv.flash.xml.jaxen; 52 53 import org.openlaszlo.iv.flash.context.Context; 54 import org.openlaszlo.iv.flash.context.XMLContext; 55 import java.util.ArrayList ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 import org.jaxen.JaxenException; 59 import org.jaxen.dom.DOMXPath; 60 import org.jaxen.dom.DOMXPath; 61 import org.w3c.dom.DOMException ; 62 import org.w3c.dom.Node ; 63 64 65 72 public class XMLContextImpl extends XMLContext { 73 74 80 public XMLContextImpl(Context parent, Node node) { 81 super(parent, node); 82 } 83 84 91 public String getValue(String path) { 92 try { 93 DOMXPath xpath = new DOMXPath(path); 94 Object value = xpath.evaluate(node); 95 if (value != null) { 96 if (value instanceof List ) { 97 List values = (List )value; 98 if (values.size() == 0) { 99 } 101 else if (values.size() == 1) { 102 return getNodeData((Node )values.get(0)); 103 } 104 else { 105 Iterator iter = values.iterator(); 106 StringBuffer sb = new StringBuffer (); 107 while (iter.hasNext()) 108 sb.append(getNodeData((Node )iter.next())); 109 return sb.toString(); 110 } 111 } 112 else 113 return value.toString(); 114 } 115 } catch (DOMException e) { 116 } catch (JaxenException e) { 117 } 118 119 return getValueFromParent(path); 120 } 121 122 private String getNodeData(Node node) { 123 switch (node.getNodeType()) { 124 case Node.DOCUMENT_FRAGMENT_NODE: 125 case Node.DOCUMENT_NODE: { 126 Node child = node.getFirstChild(); 127 if (child != null) 128 return getNodeData(child); 129 } 130 break; 131 case Node.ELEMENT_NODE: { 132 StringBuffer sb = new StringBuffer (); 133 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) 134 sb.append(getNodeData(child)); 135 return sb.toString(); 136 } 137 case Node.TEXT_NODE: 138 case Node.CDATA_SECTION_NODE: 139 case Node.ATTRIBUTE_NODE: 140 case Node.PROCESSING_INSTRUCTION_NODE : 141 return node.getNodeValue(); 142 default: 143 break; 144 } 145 return ""; 146 } 147 148 156 public List getValueList(String path) { 157 try { 158 DOMXPath xpath = new DOMXPath(path); 159 List nodes = xpath.selectNodes(node); 160 161 int size = nodes.size(); 162 if (size > 0) { 163 ArrayList contexts = new ArrayList (size); 164 for (int i = 0; i < size; i++) 165 contexts.add(new XMLContextImpl(this, (Node )nodes.get(i))); 166 return contexts; 167 } 168 } catch (JaxenException e) { 169 } 170 171 return getValueListFromParent(path); 172 } 173 } 174 | Popular Tags |