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.io.StringWriter;
013: import java.util.ArrayList;
014: import java.util.Collections;
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.Iterator;
018: import javax.xml.parsers.ParserConfigurationException;
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Element;
021: import org.w3c.dom.Node;
022: import org.w3c.dom.NodeList;
023: import org.xml.sax.SAXException;
024:
025: //project specific classes
026: import org.jfolder.common.UnexpectedSystemException;
027: import org.jfolder.common.utils.misc.MiscHelper;
028: import org.jfolder.common.utils.xml.XMLHelper;
029:
030: //other classes
031:
032: public class SimpleVirtualFileSystemProperties extends
033: BaseVirtualFileSystemProperties {
034:
035: private final static String PROPERTIES = "PROPERTIES";
036: private final static String PROPERTY = "PROPERTY";
037: private final static String DOCUMENT = "DOCUMENT";
038: private final static String NAME = "NAME";
039: private final static String VALUE = "VALUE";
040:
041: //
042: public SimpleVirtualFileSystemProperties() {
043: super ();
044: }
045:
046: public final static SimpleVirtualFileSystemProperties newInstance() {
047:
048: SimpleVirtualFileSystemProperties outValue = null;
049:
050: outValue = new SimpleVirtualFileSystemProperties();
051:
052: return outValue;
053: }
054:
055: public final static SimpleVirtualFileSystemProperties newInstance(
056: byte inContent[]) {
057:
058: try {
059: SimpleVirtualFileSystemProperties outValue = null;
060:
061: outValue = new SimpleVirtualFileSystemProperties();
062:
063: //
064: String docText = MiscHelper.fromBytesToString(inContent);
065: Document doc = XMLHelper.loadDocument(docText);
066:
067: //
068: Element rootElement = doc.getDocumentElement();
069: NodeList parentNl = rootElement.getChildNodes();
070: for (int i = 0; i < parentNl.getLength(); i++) {
071: Node nextParentNode = parentNl.item(i);
072: if (nextParentNode instanceof Element) {
073: Element nextParentElement = (Element) nextParentNode;
074: if (nextParentElement.getTagName().equals(PROPERTY)) {
075: String nextName = null;
076: String nextValue = null;
077: NodeList nextNl = nextParentElement
078: .getChildNodes();
079: for (int j = 0; j < nextNl.getLength(); j++) {
080: Node nextNode = nextNl.item(j);
081: if (nextNode instanceof Element) {
082: Element nextEle = (Element) nextNode;
083: if (nextEle.getTagName().equals(NAME)) {
084: nextName = XMLHelper
085: .getText(nextEle);
086: } else if (nextEle.getTagName().equals(
087: VALUE)) {
088: nextValue = XMLHelper
089: .getText(nextEle);
090: } else {
091: throw new UnexpectedSystemException(
092: "Unknown Element - "
093: + nextEle
094: .getTagName());
095: }
096: }
097: }
098: //
099: outValue.createProperty(nextName, nextValue);
100: }
101: }
102: }
103:
104: return outValue;
105: } catch (ParserConfigurationException pce) {
106: throw new UnexpectedSystemException(pce);
107: } catch (SAXException saxe) {
108: throw new UnexpectedSystemException(saxe);
109: } catch (IOException ioe) {
110: throw new UnexpectedSystemException(ioe);
111: }
112: }
113:
114: public byte[] toByteArray() {
115:
116: try {
117: byte outValue[] = null;
118:
119: Document doc = XMLHelper.createBlankDocument();
120: Element rootElement = doc.createElement(PROPERTIES);
121: doc.appendChild(rootElement);
122: Element docElement = doc.createElement(DOCUMENT);
123: rootElement.appendChild(docElement);
124: docElement.appendChild(doc.createTextNode(getDocument()));
125: //
126: ArrayList propNames = new ArrayList();
127: for (int i = 0; i < getPropertyCount(); i++) {
128: propNames.add(getPropertyName(i));
129: }
130: Collections.sort(propNames);
131: //
132: for (int i = 0; i < propNames.size(); i++) {
133: String nextPropName = (String) propNames.get(i);
134: String nextPropValue = getPropertyValue(nextPropName);
135: //
136: Element nextPropEle = doc.createElement(PROPERTY);
137: rootElement.appendChild(nextPropEle);
138: //
139: Element nextNameEle = doc.createElement(NAME);
140: nextPropEle.appendChild(nextNameEle);
141: nextNameEle.appendChild(doc
142: .createTextNode(nextPropName));
143: //
144: Element nextValueEle = doc.createElement(VALUE);
145: nextPropEle.appendChild(nextValueEle);
146: nextValueEle.appendChild(doc
147: .createTextNode(nextPropValue));
148: }
149:
150: StringWriter sw = new StringWriter();
151: XMLHelper.writeDocument(doc, sw);
152: outValue = MiscHelper.fromStringToBytes(sw.toString());
153:
154: return outValue;
155: } catch (ParserConfigurationException pce) {
156: throw new UnexpectedSystemException(pce);
157: } catch (IOException ioe) {
158: throw new UnexpectedSystemException(ioe);
159: }
160: }
161:
162: //
163: public void setDocument(String inDocument) {
164:
165: throw UnexpectedSystemException.notImplemented();
166: }
167:
168: public String getDocument() {
169:
170: return DEFAULT_DOCUMENT;
171: }
172:
173: ////////////////////////////////
174: public int hashCode() {
175:
176: int outValue = 0;
177:
178: for (int i = 0; i < this .getPropertyCount(); i++) {
179: outValue += this .getPropertyName(i).hashCode();
180: outValue += this .getPropertyValue(i).hashCode();
181: }
182:
183: outValue += this .getDocument().hashCode();
184:
185: return outValue;
186: }
187:
188: public boolean equals(Object inObject) {
189:
190: boolean outValue = true;
191:
192: if (inObject instanceof VirtualFileSystemProperties) {
193: VirtualFileSystemProperties otherVfsp = (VirtualFileSystemProperties) inObject;
194: //
195: if (this .getPropertyCount() == otherVfsp.getPropertyCount()) {
196: for (int i = 0; i < this .getPropertyCount(); i++) {
197: String nextPropName = this .getPropertyName(i);
198: if (otherVfsp.isPropertyPresent(nextPropName)) {
199: String nextPropValue = this .getPropertyValue(i);
200: outValue &= nextPropValue.equals(otherVfsp
201: .getPropertyValue(nextPropName));
202: } else {
203: outValue &= false;
204: }
205: }
206:
207: outValue &= this .getDocument().equals(
208: otherVfsp.getDocument());
209: } else {
210: outValue &= false;
211: }
212: } else {
213: outValue &= false;
214: }
215:
216: return outValue;
217: }
218: }
|