001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support;
014:
015: import java.util.Collection;
016: import java.util.Map;
017: import java.util.Set;
018:
019: import org.apache.xmlbeans.XmlCursor;
020: import org.apache.xmlbeans.XmlException;
021: import org.apache.xmlbeans.XmlObject;
022: import org.w3c.dom.Node;
023:
024: import com.eviware.soapui.model.testsuite.TestRunContext;
025: import com.eviware.soapui.support.types.StringToStringMap;
026: import com.eviware.soapui.support.xml.XmlUtils;
027:
028: public class XmlHolder implements Map<String, Object> {
029: private XmlObject xmlObject;
030: private StringToStringMap declaredNamespaces;
031: private TestRunContext context;
032: private String propertyRef;
033:
034: public XmlHolder(String xml) throws XmlException {
035: xmlObject = XmlObject.Factory.parse(xml);
036: }
037:
038: public XmlHolder(TestRunContext context, String propertyRef)
039: throws XmlException {
040: this (context.getProperty(propertyRef).toString());
041:
042: this .context = context;
043: this .propertyRef = propertyRef;
044: }
045:
046: public void updateProperty() {
047: updateProperty(false);
048: }
049:
050: public void updateProperty(boolean prettyPrint) {
051: if (context != null && propertyRef != null) {
052: context.setProperty(propertyRef,
053: prettyPrint ? getPrettyXml() : getXml());
054: }
055: }
056:
057: public String getNodeValue(String xpath) throws XmlException {
058: Node domNode = getDomNode(xpath);
059: return domNode == null ? null : XmlUtils.getNodeValue(domNode);
060: }
061:
062: public Map getNamespaces() {
063: if (declaredNamespaces == null)
064: declaredNamespaces = new StringToStringMap();
065:
066: return declaredNamespaces;
067: }
068:
069: public void declareNamespace(String prefix, String uri) {
070: if (declaredNamespaces == null)
071: declaredNamespaces = new StringToStringMap();
072:
073: declaredNamespaces.put(prefix, uri);
074: }
075:
076: public String[] getNodeValues(String xpath) throws XmlException {
077: xpath = initXPathNamespaces(xpath);
078:
079: XmlObject[] selectPath = xmlObject.selectPath(xpath);
080:
081: String[] result = new String[selectPath.length];
082: for (int c = 0; c < selectPath.length; c++) {
083: result[c] = XmlUtils.getNodeValue(selectPath[c]
084: .getDomNode());
085: }
086:
087: return result;
088: }
089:
090: private String initXPathNamespaces(String xpath) {
091: if (declaredNamespaces != null && !declaredNamespaces.isEmpty()) {
092: for (String prefix : declaredNamespaces.keySet()) {
093: xpath = "declare namespace " + prefix + "='"
094: + declaredNamespaces.get(prefix) + "';\n"
095: + xpath;
096: }
097: } else if (!xpath.trim().startsWith("declare namespace")) {
098: xpath = XmlUtils.declareXPathNamespaces(xmlObject) + xpath;
099: }
100: return xpath;
101: }
102:
103: public void setNodeValue(String xpath, String value)
104: throws XmlException {
105: xpath = initXPathNamespaces(xpath);
106:
107: XmlCursor cursor = xmlObject.newCursor();
108: try {
109: cursor.selectPath(xpath);
110:
111: if (cursor.toNextSelection()) {
112: XmlUtils.setNodeValue(cursor.getDomNode(), value);
113: }
114: } finally {
115: cursor.dispose();
116: }
117: }
118:
119: public XmlObject getXmlObject() {
120: return xmlObject;
121: }
122:
123: public Node getDomNode(String xpath) throws XmlException {
124: xpath = initXPathNamespaces(xpath);
125:
126: XmlCursor cursor = xmlObject.newCursor();
127: try {
128: cursor.selectPath(xpath);
129:
130: if (cursor.toNextSelection()) {
131: return cursor.getDomNode();
132: } else
133: return null;
134: } finally {
135: cursor.dispose();
136: }
137: }
138:
139: public Node[] getDomNodes(String xpath) throws XmlException {
140: xpath = initXPathNamespaces(xpath);
141:
142: XmlObject[] selectPath = xmlObject.selectPath(xpath);
143:
144: Node[] result = new Node[selectPath.length];
145: for (int c = 0; c < selectPath.length; c++) {
146: result[c] = selectPath[c].getDomNode();
147: }
148:
149: return result;
150: }
151:
152: public String getXml() {
153: return xmlObject.xmlText();
154: }
155:
156: public String getPrettyXml() {
157: return XmlUtils.prettyPrintXml(xmlObject);
158: }
159:
160: public void clear() {
161: }
162:
163: public boolean containsKey(Object key) {
164: try {
165: return getDomNode(key.toString()) != null;
166: } catch (XmlException e) {
167: e.printStackTrace();
168: return false;
169: }
170: }
171:
172: public boolean containsValue(Object value) {
173: try {
174: return getNodeValue(value.toString()) != null;
175: } catch (XmlException e) {
176: e.printStackTrace();
177: return false;
178: }
179: }
180:
181: public Set<java.util.Map.Entry<String, Object>> entrySet() {
182: return null;
183: }
184:
185: public Object get(Object key) {
186: try {
187: String str = key.toString();
188: if (str.equals("prettyXml"))
189: return getPrettyXml();
190: else if (str.equals("xmlObject"))
191: return getXmlObject();
192: else if (str.equals("namespaces"))
193: return getNamespaces();
194: else if (str.equals("xml"))
195: return getXml();
196:
197: String[] nodeValues = getNodeValues(str);
198: return nodeValues != null && nodeValues.length == 1 ? nodeValues[0]
199: : nodeValues;
200: } catch (XmlException e) {
201: e.printStackTrace();
202: return null;
203: }
204: }
205:
206: public boolean isEmpty() {
207: return false;
208: }
209:
210: public Set<String> keySet() {
211: return null;
212: }
213:
214: public String put(String key, Object value) {
215: try {
216: String result = getNodeValue(key);
217: setNodeValue(key, value == null ? null : value.toString());
218: return result;
219: } catch (XmlException e) {
220: e.printStackTrace();
221: return null;
222: }
223: }
224:
225: public void putAll(Map<? extends String, ? extends Object> t) {
226: if (t.keySet() == null)
227: return;
228:
229: for (String key : t.keySet()) {
230: put(key, t.get(key));
231: }
232: }
233:
234: public Object remove(Object key) {
235: try {
236: Node node = getDomNode(key.toString());
237: if (node != null) {
238: node.getParentNode().removeChild(node);
239: }
240: } catch (XmlException e) {
241: e.printStackTrace();
242: }
243:
244: return null;
245: }
246:
247: public int size() {
248: return 0;
249: }
250:
251: public Collection<Object> values() {
252: return null;
253: }
254: }
|