001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.config.instance;
009:
010: //base classes
011: import java.io.IOException;
012: import java.util.HashMap;
013: import javax.xml.parsers.ParserConfigurationException;
014: import org.w3c.dom.Document;
015: import org.w3c.dom.Element;
016: import org.w3c.dom.Node;
017: import org.w3c.dom.NodeList;
018: import org.xml.sax.SAXException;
019:
020: //project specific classes
021: import org.jfolder.common.UnexpectedSystemException;
022: import org.jfolder.common.utils.misc.MiscHelper;
023: import org.jfolder.common.utils.xml.LinearXPath;
024: import org.jfolder.common.utils.xml.LinearXPathHelper;
025: import org.jfolder.common.utils.xml.XMLHelper;
026:
027: //other classes
028:
029: public class ConfigInstanceConfig {
030:
031: //
032: private String content = null;
033: private String customContent = null;
034:
035: //
036: private transient Document contentDoc = null;
037: private transient Document customContentDoc = null;
038: //
039: private transient HashMap cachedContentDoc = null;
040: private transient HashMap cachedCustomContentDoc = null;
041:
042: protected ConfigInstanceConfig(String inContent,
043: String inCustomContent) {
044: this .content = inContent;
045: this .customContent = inCustomContent;
046: }
047:
048: public final static ConfigInstanceConfig newInstance(
049: String inContent, String inCustomContent) {
050:
051: ConfigInstanceConfig outValue = null;
052:
053: outValue = new ConfigInstanceConfig(inContent, inCustomContent);
054:
055: return outValue;
056: }
057:
058: private void init() {
059: try {
060: MiscHelper.println("ConInstCon conD = " + this .content);
061: MiscHelper.println("ConInstCon cusConD = "
062: + this .customContent);
063: if (this .contentDoc == null) {
064: this .contentDoc = XMLHelper.loadDocument(this .content);
065: }
066: if (this .customContentDoc == null) {
067: this .customContentDoc = XMLHelper
068: .loadDocument(this .customContent);
069: }
070: if (this .cachedContentDoc == null) {
071: this .cachedContentDoc = new HashMap();
072: }
073: if (this .cachedCustomContentDoc == null) {
074: this .cachedCustomContentDoc = new HashMap();
075: }
076: } catch (SAXException saxe) {
077: throw new UnexpectedSystemException(saxe);
078: } catch (IOException ioe) {
079: throw new UnexpectedSystemException(ioe);
080: } catch (ParserConfigurationException pce) {
081: throw new UnexpectedSystemException(pce);
082: }
083: }
084:
085: //
086: public String getMandatoryProperty(boolean inCustom,
087: LinearXPath inProp) {
088:
089: String outValue = null;
090: init();
091: Element e = getCachedElement(inCustom, inProp);
092:
093: if (e == null) {
094: throw new UnexpectedSystemException("Property '" + inProp
095: + "' is not present");
096: }
097:
098: outValue = XMLHelper.getText(e);
099: return outValue;
100: }
101:
102: public String getMandatoryPropertyAttribute(boolean inCustom,
103: LinearXPath inProp, String inAttr) {
104:
105: String outValue = null;
106: init();
107: Element e = getCachedElement(inCustom, inProp);
108:
109: if (e == null) {
110: throw new UnexpectedSystemException("Property '" + inProp
111: + "' is not present");
112: }
113:
114: if (e.getAttributeNode(inAttr) == null) {
115: throw new UnexpectedSystemException("Attr '" + inAttr
116: + "' of prop '" + inAttr + "' is not present");
117: }
118:
119: outValue = e.getAttribute(inAttr);
120: return outValue;
121: }
122:
123: public int getMandatoryPropertyCount(boolean inCustom,
124: LinearXPath inProp, String inName) {
125:
126: int outValue = 0;
127: init();
128: Element e = getCachedElement(inCustom, inProp);
129:
130: if (e == null) {
131: throw new UnexpectedSystemException("Property '" + inProp
132: + "' is not present");
133: }
134:
135: NodeList nl = e.getChildNodes();
136:
137: for (int i = 0; i < nl.getLength(); i++) {
138: Node nextNode = nl.item(i);
139: if (nextNode instanceof Element) {
140: Element nextElement = (Element) nextNode;
141: if (nextElement.getTagName().equals(inName)) {
142: outValue++;
143: }
144: }
145: }
146: return outValue;
147: }
148:
149: //
150: public String getProperty(boolean inCustom, LinearXPath inProp,
151: String inDefault) {
152:
153: String outValue = inDefault;
154: init();
155: Element e = getCachedElement(inCustom, inProp);
156:
157: if (e == null) {
158: } else {
159: outValue = XMLHelper.getText(e);
160: }
161: return outValue;
162: }
163:
164: public String getPropertyAttribute(boolean inCustom,
165: LinearXPath inProp, String inAttr, String inDefault) {
166:
167: String outValue = inDefault;
168: init();
169: Element e = getCachedElement(inCustom, inProp);
170:
171: if (e == null) {
172: } else {
173: if (e.getAttributeNode(inAttr) == null) {
174: } else {
175: outValue = e.getAttribute(inAttr);
176: }
177: }
178: return outValue;
179: }
180:
181: public int getPropertyCount(boolean inCustom, LinearXPath inProp,
182: String inName, int inDefault) {
183:
184: int outValue = inDefault;
185: init();
186: Element e = getCachedElement(inCustom, inProp);
187:
188: if (e == null) {
189: } else {
190: NodeList nl = e.getChildNodes();
191: outValue = 0;
192: for (int i = 0; i < nl.getLength(); i++) {
193: Node nextNode = nl.item(i);
194: if (nextNode instanceof Element) {
195: Element nextElement = (Element) nextNode;
196: if (nextElement.getTagName().equals(inName)) {
197: outValue++;
198: }
199: }
200: }
201: }
202: return outValue;
203: }
204:
205: //
206: public boolean isPropertyPresent(boolean inCustom,
207: LinearXPath inProp) {
208:
209: boolean outValue = false;
210: init();
211: Element e = getCachedElement(inCustom, inProp);
212: outValue = (e != null);
213: return outValue;
214: }
215:
216: private Element getCachedElement(boolean inCustom,
217: LinearXPath inProp) {
218:
219: Element outValue = null;
220:
221: String propName = inProp.toString();
222:
223: Document doc = null;
224: HashMap props = null;
225:
226: if (inCustom) {
227: doc = this .customContentDoc;
228: props = this .cachedCustomContentDoc;
229: } else {
230: doc = this .contentDoc;
231: props = this .cachedContentDoc;
232: }
233:
234: if (props.containsKey(inProp)) {
235: outValue = (Element) props.get(propName);
236: } else {
237: outValue = LinearXPathHelper.getXPathElement(doc, inProp);
238: props.put(propName, outValue);
239: }
240:
241: //MiscHelper.println("<!---------- ConfigInstanceConfig ---------->");
242: //MiscHelper.println("outValue = " + outValue);
243: //MiscHelper.println("doc = " + doc);
244: //MiscHelper.println("inProp = " + inProp);
245: //MiscHelper.println("inCustom = " + inCustom);
246: //MiscHelper.println("content = " + this.content);
247: //MiscHelper.println("customContent = " + this.customContent);
248: //MiscHelper.println("<!------------------------------------------>");
249:
250: return outValue;
251: }
252:
253: }
|