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.parser.dom4j;
046:
047: import org.dom4j.Element;
048: import org.dom4j.QName;
049: import org.dom4j.io.OutputFormat;
050: import org.dom4j.io.XMLWriter;
051: import org.obe.util.DateUtilities;
052: import org.obe.xpdl.model.misc.Duration;
053: import org.obe.xpdl.model.misc.DurationUnit;
054: import org.obe.xpdl.parser.XPDLParserException;
055:
056: import java.io.IOException;
057: import java.io.StringWriter;
058: import java.net.MalformedURLException;
059: import java.net.URL;
060: import java.text.ParseException;
061: import java.util.Date;
062: import java.util.List;
063:
064: public class Util {
065: /**
066: * Return the child element with the given name. The element must be in the
067: * same name space as the parent element.
068: *
069: * @param element The parent element
070: * @param name The child element name
071: * @return The child element
072: */
073: public static Element child(Element element, String name) {
074: // Now that we're using a default namespace, none of the XPDL elements
075: // have a qualified name.
076: // return element.element(new QName(name, element.getNamespace()));
077: return element.element(name);
078: }
079:
080: /**
081: * Return the child elements with the given name. The elements must be in
082: * the same name space as the parent element.
083: *
084: * @param element The parent element
085: * @param name The child element name
086: * @return The child elements
087: */
088: public static List children(Element element, String name) {
089: // Now that we're using a default namespace, none of the XPDL elements
090: // have a qualified name.
091: // return element.elements(new QName(name, element.getNamespace()));
092: return element.elements(name);
093: }
094:
095: /**
096: * Detaches an element from its parent and returns a child node.
097: *
098: * @param element The element to detach.
099: * @param qname The qualified name of the child element to return.
100: * @return The child element.
101: */
102: public static Element detach(Element element, QName qname) {
103: element.detach();
104: return element.element(qname);
105: }
106:
107: // Conversion
108:
109: /**
110: * Return the value of the child element with the given name. The element
111: * must be in the same name space as the parent element.
112: *
113: * @param element The parent element
114: * @param name The child element name
115: * @return The child element value
116: */
117: public static String elementAsString(Element element, String name) {
118: String s = element.elementTextTrim(new QName(name, element
119: .getNamespace()));
120: return s == null || s.length() == 0 ? null : s;
121: }
122:
123: public static Date elementAsDate(Element element, String name)
124: throws XPDLParserException {
125:
126: String text = elementAsString(element, name);
127: if (text == null)
128: return null;
129:
130: try {
131: return DateUtilities.getInstance().parse(text);
132: } catch (ParseException e) {
133: throw new XPDLParserException(
134: "Error parsing date: " + text, e);
135: }
136: }
137:
138: public static int elementAsInteger(Element element, String name) {
139: String text = elementAsString(element, name);
140: if (text == null)
141: return 0;
142:
143: return Integer.parseInt(text);
144: }
145:
146: public static boolean elementAsBoolean(Element element, String name) {
147: String text = elementAsString(element, name);
148: if (text == null)
149: return false;
150:
151: return Boolean.valueOf(text).booleanValue();
152: }
153:
154: public static URL elementAsURL(Element element, String name)
155: throws XPDLParserException {
156:
157: String text = elementAsString(element, name);
158: if (text == null)
159: return null;
160:
161: try {
162: return new URL(text);
163: } catch (MalformedURLException e) {
164: throw new XPDLParserException("Invalid URL: " + text, e);
165: }
166: }
167:
168: public static Duration elementAsDuration(Element element,
169: String name) throws XPDLParserException {
170:
171: String text = elementAsString(element, name);
172: if (text == null)
173: return null;
174:
175: try {
176: return Duration.valueOf(text);
177: } catch (NumberFormatException e) {
178: throw new XPDLParserException("Error parsing duration", e);
179: }
180: }
181:
182: public static DurationUnit elementAsDurationUnit(Element element,
183: String name) throws XPDLParserException {
184:
185: String text = elementAsString(element, name);
186: if (text == null)
187: return null;
188:
189: try {
190: return DurationUnit.valueOf(text);
191: } catch (NumberFormatException e) {
192: throw new XPDLParserException(
193: "Error parsing duration unit", e);
194: }
195: }
196:
197: public static String exportToText(Element element)
198: throws XPDLParserException {
199:
200: StringWriter out = new StringWriter();
201: OutputFormat format = new OutputFormat(" ", true);
202: format.setTrimText(true);
203: XMLWriter writer = new XMLWriter(out, format);
204: try {
205: writer.write(element);
206: } catch (IOException e) {
207: throw new XPDLParserException(e);
208: }
209: out.flush();
210: return out.toString();
211: }
212:
213: private Util() {
214: }
215: }
|