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.xml;
014:
015: import org.apache.xmlbeans.XmlCursor;
016: import org.apache.xmlbeans.XmlObject;
017:
018: /**
019: * Support class for reading XmlObject based configurations..
020: *
021: * @author Ole.Matzura
022: */
023:
024: public class XmlObjectConfigurationReader {
025: private final XmlObject config;
026:
027: public XmlObjectConfigurationReader(XmlObject config) {
028: this .config = config;
029: }
030:
031: public int readInt(String name, int def) {
032: if (config == null)
033: return def;
034:
035: try {
036: return Integer.parseInt(readString(name, null));
037: } catch (NumberFormatException e) {
038: }
039:
040: return def;
041: }
042:
043: public long readLong(String name, int def) {
044: if (config == null)
045: return def;
046:
047: try {
048:
049: return Long.parseLong(readString(name, null));
050: } catch (NumberFormatException e) {
051: }
052:
053: return def;
054: }
055:
056: public float readFloat(String name, float def) {
057: if (config == null)
058: return def;
059:
060: try {
061: return Float.parseFloat(readString(name, null));
062: } catch (NumberFormatException e) {
063: }
064:
065: return def;
066: }
067:
068: public String readString(String name, String def) {
069: if (config == null)
070: return def;
071:
072: XmlObject[] paths = config.selectPath("$this/" + name);
073: if (paths.length == 1) {
074: XmlCursor cursor = paths[0].newCursor();
075: String textValue = cursor.getTextValue();
076: cursor.dispose();
077: return textValue;
078: }
079:
080: return def;
081: }
082:
083: public String[] readStrings(String name) {
084: if (config == null)
085: return null;
086:
087: XmlObject[] paths = config.selectPath("$this/" + name);
088: String[] result = new String[paths.length];
089:
090: for (int c = 0; c < paths.length; c++) {
091: XmlCursor cursor = paths[c].newCursor();
092: result[c] = cursor.getTextValue();
093: cursor.dispose();
094: }
095:
096: return result;
097: }
098:
099: public boolean readBoolean(String name, boolean def) {
100: try {
101: return Boolean
102: .valueOf(readString(name, String.valueOf(def)));
103: } catch (Exception e) {
104: return def;
105: }
106: }
107: }
|