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.common.files;
009:
010: //base classes
011: import java.io.IOException;
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import javax.xml.parsers.ParserConfigurationException;
017: import org.xml.sax.SAXException;
018:
019: //project specific classes
020: import org.jfolder.common.UnexpectedSystemException;
021: import org.jfolder.common.utils.misc.MiscHelper;
022: import org.jfolder.common.utils.xml.XMLHelper;
023:
024: //other classes
025:
026: public abstract class BaseVirtualFileSystemProperties implements
027: VirtualFileSystemProperties {
028:
029: //
030: private final static String VALID_PROPERTY_NAME = ".*";
031: private final static String VALID_PROPERTY_VALUE = ".*";
032:
033: //
034: private HashMap propertyNames = null;
035: private HashMap propertyValues = null;
036: private ArrayList propertyNamesList = null;
037:
038: //
039: public BaseVirtualFileSystemProperties() {
040: this .propertyNames = new HashMap();
041: this .propertyValues = new HashMap();
042: this .propertyNamesList = new ArrayList();
043: }
044:
045: //
046: //public void setDocument(String inDocument) {
047: //
048: // throw UnexpectedSystemException.notImplemented();
049: //}
050: //public String getDocument() {
051: //
052: // return DEFAULT_DOCUMENT;
053: //}
054: //
055: public int getPropertyCount() {
056: return this .propertyNames.size();
057: }
058:
059: public String getPropertyValue(String inName) {
060:
061: String outValue = null;
062:
063: if (isPropertyPresent(inName)) {
064: outValue = (String) this .propertyValues.get(inName
065: .toUpperCase());
066: } else {
067: throw new UnexpectedSystemException("Property '" + inName
068: + "' does not exist");
069: }
070:
071: return outValue;
072: }
073:
074: //
075: public String getPropertyName(int inIndex) {
076:
077: String outValue = null;
078:
079: if (inIndex < getPropertyCount()) {
080: String propertyName = (String) this .propertyNamesList
081: .get(inIndex);
082: outValue = (String) this .propertyNames.get(propertyName);
083: } else {
084: throw new UnexpectedSystemException(
085: "No property at index '" + inIndex + "'");
086: }
087:
088: return outValue;
089: }
090:
091: public String getPropertyValue(int inIndex) {
092:
093: String outValue = null;
094:
095: if (inIndex < getPropertyCount()) {
096: String propertyName = (String) this .propertyNamesList
097: .get(inIndex);
098: outValue = (String) this .propertyValues.get(propertyName);
099: } else {
100: throw new UnexpectedSystemException(
101: "No property at index '" + inIndex + "'");
102: }
103:
104: return outValue;
105: }
106:
107: //
108: public boolean isPropertyPresent(String inName) {
109:
110: boolean outValue = false;
111:
112: outValue = this .propertyNamesList
113: .contains(inName.toUpperCase());
114: //for (int i = 0; i < getPropertyCount(); i++) {
115: // String nextName = getPropertyName(i);
116: // if (nextName.equalsIgnoreCase(inName)) {
117: // outValue = true;
118: // }
119: //}
120:
121: return outValue;
122: }
123:
124: //
125: public boolean isValidPropertyName(String inName) {
126:
127: boolean outValue = false;
128:
129: outValue = MiscHelper.isRegularExpressionMatch(inName,
130: VALID_PROPERTY_NAME, false);
131:
132: return outValue;
133: }
134:
135: public boolean isValidPropertyValue(String inValue) {
136:
137: boolean outValue = false;
138:
139: outValue = MiscHelper.isRegularExpressionMatch(inValue,
140: VALID_PROPERTY_VALUE, false);
141:
142: return outValue;
143: }
144:
145: //
146: public void createProperty(String inName, String inValue) {
147: if (!isPropertyPresent(inName)) {
148: this .propertyNames.put(inName.toUpperCase(), inName);
149: this .propertyValues.put(inName.toUpperCase(), inValue);
150: this .propertyNamesList.add(inName.toUpperCase());
151: } else {
152: throw new UnexpectedSystemException("Property '" + inName
153: + "' already exists");
154: }
155: }
156:
157: public void removeProperty(String inName) {
158: if (isPropertyPresent(inName)) {
159: this .propertyNames.remove(inName.toUpperCase());
160: this .propertyValues.remove(inName.toUpperCase());
161: this .propertyNamesList.remove(inName.toUpperCase());
162: } else {
163: throw new UnexpectedSystemException("Property '" + inName
164: + "' does not exist");
165: }
166: }
167:
168: public void updateProperty(String inName, String inValue) {
169: if (isPropertyPresent(inName)) {
170: this .propertyNames.put(inName.toUpperCase(), inName);
171: this .propertyValues.put(inName.toUpperCase(), inValue);
172: } else {
173: throw new UnexpectedSystemException("Property '" + inName
174: + "' does not exist");
175: }
176: }
177:
178: //
179: public boolean isValidDocument(String inDocument) {
180:
181: boolean outValue = true;
182:
183: try {
184: XMLHelper.loadDocument(inDocument);
185: } catch (ParserConfigurationException pce) {
186: outValue = false;
187: } catch (SAXException saxe) {
188: outValue = false;
189: } catch (IOException ioe) {
190: outValue = false;
191: }
192:
193: return outValue;
194: }
195:
196: }
|