001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: Xml2Config.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.config;
009:
010: import com.uwyn.rife.config.exceptions.ConfigErrorException;
011: import com.uwyn.rife.ioc.HierarchicalProperties;
012: import com.uwyn.rife.ioc.exceptions.PropertyValueException;
013: import com.uwyn.rife.rep.Rep;
014: import com.uwyn.rife.xml.Xml2Data;
015: import com.uwyn.rife.xml.exceptions.XmlErrorException;
016: import java.util.ArrayList;
017: import java.util.HashMap;
018: import org.xml.sax.Attributes;
019:
020: public class Xml2Config extends Xml2Data {
021: private StringBuilder mCharacterDataStack = null;
022:
023: private String mCurrentListName = null;
024: private String mCurrentParameterName = null;
025:
026: private HashMap<String, String> mParameters = null;
027: private ArrayList<String> mFinalParameters = null;
028: private HashMap<String, ArrayList<String>> mLists = null;
029: private ArrayList<String> mFinalLists = null;
030:
031: public Xml2Config() {
032: this (null, null, null, null);
033: }
034:
035: public Xml2Config(HashMap<String, String> parameters,
036: ArrayList<String> finalParameters,
037: HashMap<String, ArrayList<String>> lists,
038: ArrayList<String> finalLists) {
039: mParameters = parameters;
040: mFinalParameters = finalParameters;
041: mLists = lists;
042: mFinalLists = finalLists;
043:
044: if (null == mParameters) {
045: mParameters = new HashMap<String, String>();
046: }
047: if (null == mFinalParameters) {
048: mFinalParameters = new ArrayList<String>();
049: }
050: if (null == mLists) {
051: mLists = new HashMap<String, ArrayList<String>>();
052: }
053: if (null == mFinalLists) {
054: mFinalLists = new ArrayList<String>();
055: }
056: }
057:
058: public HashMap<String, String> getParameters() {
059: return mParameters;
060: }
061:
062: public ArrayList<String> getFinalParameters() {
063: return mFinalParameters;
064: }
065:
066: public HashMap<String, ArrayList<String>> getLists() {
067: return mLists;
068: }
069:
070: public ArrayList<String> getFinalLists() {
071: return mFinalLists;
072: }
073:
074: public void startDocument() {
075: mCharacterDataStack = null;
076: mCurrentListName = null;
077: mCurrentParameterName = null;
078: }
079:
080: public void endDocument() {
081: mCharacterDataStack = null;
082: mCurrentListName = null;
083: mCurrentParameterName = null;
084: }
085:
086: public void startElement(String namespaceURI, String localName,
087: String qName, Attributes atts) {
088: if (qName.equals("param") || qName.equals("item")) {
089: mCharacterDataStack = new StringBuilder();
090: }
091:
092: if (qName.equals("config")) {
093: // do nothing
094: } else if (qName.equals("list")) {
095: String name = atts.getValue("name");
096:
097: if (!mFinalLists.contains(name)) {
098: mCurrentListName = name;
099: mLists.put(name, new ArrayList<String>());
100:
101: String final_attribute = atts.getValue("final");
102: if (final_attribute != null
103: && (final_attribute.equals("1")
104: || final_attribute.equals("t") || final_attribute
105: .equals("true"))) {
106: mFinalLists.add(name);
107: }
108: }
109: } else if (qName.equals("param")) {
110: String name = atts.getValue("name");
111:
112: if (!mFinalParameters.contains(name)) {
113: mCurrentParameterName = name;
114:
115: String final_attribute = atts.getValue("final");
116: if (final_attribute != null
117: && (final_attribute.equals("1")
118: || final_attribute.equals("t") || final_attribute
119: .equals("true"))) {
120: mFinalParameters.add(name);
121: }
122: }
123: } else if (qName.equals("item")) {
124: } else if (qName.equals("value")) {
125: String parameter_name = atts.getValue("name");
126:
127: if (mParameters.containsKey(parameter_name)) {
128: mCharacterDataStack.append(mParameters
129: .get(parameter_name));
130: }
131: } else if (qName.equals("property")) {
132: String property_name = atts.getValue("name");
133:
134: HierarchicalProperties properties = Rep.getProperties();
135: properties.contains(property_name);
136:
137: if (properties.contains(property_name)) {
138: try {
139: mCharacterDataStack.append(properties.get(
140: property_name).getValueString());
141: } catch (PropertyValueException e) {
142: throw new XmlErrorException(
143: "Error while obtain the String value of property '"
144: + property_name + "'.", e);
145: }
146: }
147: } else if (qName.equals("include")) {
148: String included_file = atts.getValue("file");
149:
150: try {
151: new Config(included_file, getResourceFinder(),
152: mParameters, mFinalParameters, mLists,
153: mFinalLists);
154: } catch (ConfigErrorException e) {
155: throw new XmlErrorException(
156: "Error while processing the included config file '"
157: + included_file + "'.", e);
158: }
159: } else {
160: throw new XmlErrorException("Unsupport element name '"
161: + qName + "'.");
162: }
163: }
164:
165: public void endElement(String namespaceURI, String localName,
166: String qName) {
167: if (qName.equals("config")) {
168: } else if (qName.equals("param")) {
169: if (mCurrentParameterName != null) {
170: String parameter_name = mCurrentParameterName;
171:
172: mParameters.put(parameter_name, mCharacterDataStack
173: .toString());
174:
175: mCurrentParameterName = null;
176: }
177:
178: mCharacterDataStack = null;
179: } else if (qName.equals("list")) {
180: mCurrentListName = null;
181: } else if (qName.equals("item")) {
182: if (mCurrentListName != null) {
183: mLists.get(mCurrentListName).add(
184: mCharacterDataStack.toString());
185: mCharacterDataStack = null;
186: }
187: } else if (qName.equals("value")) {
188: }
189: }
190:
191: public void characters(char[] ch, int start, int length) {
192: if (mCharacterDataStack != null && length > 0) {
193: mCharacterDataStack.append(String.copyValueOf(ch, start,
194: length));
195: }
196: }
197: }
|