001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.xpdl.serializer.dom4j;
046:
047: import org.dom4j.*;
048: import org.dom4j.io.SAXReader;
049: import org.obe.util.DateUtilities;
050: import org.obe.xpdl.model.misc.Duration;
051: import org.obe.xpdl.parser.dom4j.DOM4JNames;
052:
053: import java.io.StringReader;
054: import java.net.URL;
055: import java.util.Date;
056: import java.util.List;
057:
058: /**
059: * Dom4J XPDL Serializer utility class.
060: *
061: * @author Anthony Eden
062: * @author Adrian Price
063: */
064: final class Util {
065: /**
066: * Adds a child element with the specific name to the given parent element
067: * and returns the child element. This method will use the namespace of the
068: * parent element for the child element's namespace.
069: *
070: * @param parent The parent element
071: * @param name The new child element name
072: * @return The child element
073: */
074: static Element addElement(Element parent, String name) {
075: // Namespace namespace = parent.getNamespace();
076: // return namespace == Namespace.NO_NAMESPACE
077: // ? parent.addElement(name)
078: // : parent.addElement(new QName(name, namespace));
079: return parent.addElement(name);
080: }
081:
082: /**
083: * Adds a child element with the specific name and the given value to the
084: * given parent element and returns the child element. This method will use
085: * the namespace of the parent element for the child element's namespace.
086: *
087: * @param parent The parent element
088: * @param name The new child element name
089: * @param value The value
090: * @return The child element
091: */
092: static Element addElement(Element parent, String name, Date value) {
093: return addElement(parent, name, value, null);
094: }
095:
096: /**
097: * Adds a child element with the specific name and the given value to the
098: * given parent element and returns the child element. This method will use
099: * the namespace of the parent element for the child element's namespace.
100: * If the given value is null then the default value is used. If the value
101: * is null then this method will not add the child element and will return
102: * null.
103: *
104: * @param parent The parent element
105: * @param name The new child element name
106: * @param value The value
107: * @param defaultValue The default value (if the value is null)
108: * @return The child element
109: */
110: static Element addElement(Element parent, String name, Date value,
111: Date defaultValue) {
112:
113: Element child = null;
114:
115: if (value == null)
116: value = defaultValue;
117:
118: if (value != null) {
119: child = addElement(parent, name);
120: child.addText(DateUtilities.getInstance().format(value));
121: }
122:
123: return child;
124: }
125:
126: /**
127: * Adds a child element with the specific name and the given value to the
128: * given parent element and returns the child element. This method will use
129: * the namespace of the parent element for the child element's namespace.
130: *
131: * @param parent The parent element
132: * @param name The new child element name
133: * @param value The value
134: * @return The child element
135: */
136: static Element addElement(Element parent, String name, String value) {
137: return addElement(parent, name, value, null);
138: }
139:
140: /**
141: * Adds a child element with the specific name and the given value to the
142: * given parent element and returns the child element. This method will use
143: * the namespace of the parent element for the child element's namespace.
144: * If the given value is null then the default value is used. If the value
145: * is null then this method will not add the child element and will return
146: * null.
147: *
148: * @param parent The parent element
149: * @param name The new child element name
150: * @param value The value
151: * @param defaultValue The default value (if the value is null)
152: * @return The child element
153: */
154: static Element addElement(Element parent, String name,
155: String value, String defaultValue) {
156:
157: Element child = null;
158:
159: if (value == null)
160: value = defaultValue;
161:
162: if (value != null) {
163: child = addElement(parent, name);
164: child.addText(value);
165: }
166:
167: return child;
168: }
169:
170: /**
171: * Adds a child element with the specific name and the given value to the
172: * given parent element and returns the child element. This method will use
173: * the namespace of the parent element for the child element's namespace.
174: *
175: * @param parent The parent element
176: * @param name The new child element name
177: * @param value The value
178: * @return The child element
179: */
180: static Element addElement(Element parent, String name, URL value) {
181: return addElement(parent, name, value, null);
182: }
183:
184: /**
185: * Adds a child element with the specific name and the given value to the
186: * given parent element and returns the child element. This method will use
187: * the namespace of the parent element for the child element's namespace.
188: * If the given value is null then the default value is used. If the value
189: * is null then this method will not add the child element and will return
190: * null.
191: *
192: * @param parent The parent element
193: * @param name The new child element name
194: * @param value The value
195: * @param defaultValue The default value (if the value is null)
196: * @return The child element
197: */
198: static Element addElement(Element parent, String name, URL value,
199: URL defaultValue) {
200: Element child = null;
201:
202: if (value == null)
203: value = defaultValue;
204:
205: if (value != null) {
206: child = addElement(parent, name);
207: child.addText(value.toString());
208: }
209:
210: return child;
211: }
212:
213: /**
214: * Adds a child element with the specific name and the given value to the
215: * given parent element and returns the child element. This method will use
216: * the namespace of the parent element for the child element's namespace.
217: *
218: * @param parent The parent element
219: * @param name The new child element name
220: * @param value The value
221: * @return The child element
222: */
223: static Element addElement(Element parent, String name,
224: Duration value) {
225: return addElement(parent, name, value, null);
226: }
227:
228: /**
229: * Adds a child element with the specific name and the given value to the
230: * given parent element and returns the child element. This method will use
231: * the namespace of the parent element for the child element's namespace.
232: * If the given value is null then the default value is used. If the value
233: * is null then this method will not add the child element and will return
234: * null.
235: *
236: * @param parent The parent element
237: * @param name The new child element name
238: * @param value The value
239: * @param defaultValue The default value (if the value is null)
240: * @return The child element
241: */
242: static Element addElement(Element parent, String name,
243: Duration value, Duration defaultValue) {
244: Element child = null;
245:
246: if (value == null)
247: value = defaultValue;
248:
249: if (value != null) {
250: child = addElement(parent, name);
251: child.addText(value.toString());
252: }
253:
254: return child;
255: }
256:
257: static void importFromText(String text, Element parent)
258: throws DocumentException {
259:
260: if (text == null || text.length() == 0)
261: return;
262:
263: SAXReader reader = new SAXReader(DocumentFactory.getInstance());
264: reader.setStripWhitespaceText(true);
265: reader.setMergeAdjacentText(true);
266: Document document = reader.read(new StringReader(text));
267: Element docElem = document.getRootElement();
268: Namespace ns = docElem.getNamespace();
269: normalizeNSPrefix(docElem,
270: ns == Namespace.NO_NAMESPACE ? DOM4JNames.XPDL_NS : ns);
271: parent.add(docElem.detach());
272: }
273:
274: static void normalizeNSPrefix(Element elem, Namespace ns) {
275: Namespace elemNS = elem.getNamespace();
276: /*if (elemNS.equals(ns))
277: elem.setQName(new QName(elem.getName(), ns));
278: else*/
279: if (!elemNS.equals(ns)
280: && elem.getNamespaceURI().equals(ns.getURI())) {
281:
282: elem.setQName(new QName(elem.getName(), ns));
283: }
284: List children = elem.elements();
285: for (int i = 0; i < children.size(); i++) {
286: Element child = (Element) children.get(i);
287: normalizeNSPrefix(child, ns);
288: }
289: }
290:
291: private Util() {
292: }
293: }
|