001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.vfs.metadata;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.w3c.dom.Element;
026: import org.w3c.dom.Node;
027: import org.w3c.dom.NodeList;
028: import org.w3c.dom.Text;
029:
030: /**
031: * Domains describe the applicability of properties by stating the paths
032: * that they are allowed to be used in and the min and max occurrence values
033: * for that path and any resource/content type restrictions. Domains are
034: * stored as metadata on the {@link org.openharmonise.vfs.VirtualFile}s for {@link Property} objects.
035: *
036: * @author Matthew Large
037: * @version $Revision: 1.1 $
038: *
039: */
040: public class Domain {
041:
042: /**
043: * Allowed resource type.
044: */
045: private String m_sResourceType = null;
046:
047: /**
048: * Allowed content types.
049: */
050: private ArrayList m_aContentTypes = new ArrayList(3);
051:
052: /**
053: * Allowed paths.
054: */
055: private ArrayList m_aPaths = new ArrayList(3);
056:
057: /**
058: * Minimum occurrence.
059: */
060: private int m_nMinOccurs = 0;
061:
062: /**
063: * Maximum occurrence. A value of -1 represents UNBOUNDED.
064: */
065: private int m_nMaxOccurs = -1;
066:
067: /**
068: * Constructs a new Domain.
069: */
070: public Domain() {
071: super ();
072: }
073:
074: /**
075: * Returns the maximum occurrence.
076: *
077: * @return Maximum Occurrence
078: */
079: public int getMaxOccurs() {
080: return m_nMaxOccurs;
081: }
082:
083: /**
084: * Returns the minimum occurrence.
085: *
086: * @return Minimum occurrence
087: */
088: public int getMinOccurs() {
089: return m_nMinOccurs;
090: }
091:
092: /**
093: * Returns a list of the allowed content types.
094: *
095: * @return Content types
096: */
097: public List getContentTypes() {
098: return (List) m_aContentTypes.clone();
099: }
100:
101: /**
102: * Allowed resource type.
103: *
104: * @return Resource type
105: */
106: public String getResourceType() {
107: return m_sResourceType;
108: }
109:
110: /**
111: * Sets the maximum occurrence.
112: *
113: * @param i Maximum occurrence
114: */
115: public void setMaxOccurs(int i) {
116: m_nMaxOccurs = i;
117: }
118:
119: /**
120: * Sets the minimum occurrence.
121: *
122: * @param i Minimum occurrence
123: */
124: public void setMinOccurs(int i) {
125: m_nMinOccurs = i;
126: }
127:
128: /**
129: * Adds an allowed content type.
130: *
131: * @param string Content type
132: */
133: public void addContentType(String string) {
134: m_aContentTypes.add(string);
135: }
136:
137: /**
138: * Sets the allowed resource type.
139: *
140: * @param string Resource type
141: */
142: public void setResourceType(String string) {
143: m_sResourceType = string;
144: }
145:
146: /**
147: * Adds an allowed path.
148: *
149: * @param sPath Full path
150: */
151: public void addPath(String sPath) {
152: this .m_aPaths.add(sPath);
153: }
154:
155: /**
156: * Returns a list of all the allowed paths.
157: *
158: * @return Full paths
159: */
160: public List getPaths() {
161: return (List) this .m_aPaths.clone();
162: }
163:
164: /**
165: * Validates a list of values against the restrictions in this domain.
166: *
167: * @param aValues List of values to be validated
168: * @return Results of the validation
169: */
170: public ValidationResult validate(List aValues) {
171: ValidationResult result = new ValidationResult();
172:
173: if (aValues.size() < this .m_nMinOccurs) {
174: result.setValid(false);
175: result
176: .setMessage(this .m_nMinOccurs
177: + " is the minimum number of values required for this property.");
178: }
179:
180: if (this .m_nMaxOccurs > -1
181: && aValues.size() > this .m_nMaxOccurs) {
182: result.setValid(false);
183: result
184: .setMessage(this .m_nMaxOccurs
185: + " is the maximum number of values allowed for this property.");
186: }
187:
188: return result;
189: }
190:
191: /**
192: * Populates the domain from a XML element.
193: *
194: * @param elDomain Root element of domain XML
195: */
196: public void instantiate(Element elDomain) {
197: NodeList nl = elDomain.getElementsByTagNameNS("DAV:", "href");
198: for (int i = 0; i < nl.getLength(); i++) {
199: Element elPath = (Element) nl.item(i);
200: if (elPath.getChildNodes().getLength() == 1) {
201: Node node = elPath.getFirstChild();
202: if (node.getNodeType() == Node.TEXT_NODE) {
203: this .m_aPaths.add(((Text) node).getNodeValue());
204: }
205: }
206: }
207:
208: nl = elDomain.getElementsByTagNameNS("DAV:", "resourcetype");
209: for (int i = 0; i < nl.getLength(); i++) {
210: Element elResourceType = (Element) nl.item(i);
211: if (elResourceType.getChildNodes().getLength() > 0) {
212: NodeList nl2 = elResourceType.getChildNodes();
213: for (int j = 0; j < nl2.getLength(); j++) {
214: //this.m_sResourceType = nl2.item(j).getNodeName();
215: if (nl2.item(j).getNodeType() == Node.ELEMENT_NODE) {
216: this .m_sResourceType = ((Element) nl2.item(j))
217: .getTagName();
218: }
219: }
220: }
221: }
222:
223: nl = elDomain.getElementsByTagNameNS("DAV:", "contenttype");
224: for (int i = 0; i < nl.getLength(); i++) {
225: Element elContentType = (Element) nl.item(i);
226: if (elContentType.getChildNodes().getLength() == 1) {
227: Node node = elContentType.getFirstChild();
228: if (node.getNodeType() == Node.TEXT_NODE) {
229: this .m_aContentTypes.add(((Text) node)
230: .getNodeValue());
231: }
232: }
233: }
234:
235: nl = elDomain.getElementsByTagNameNS("DAV:", "minOccurs");
236: for (int i = 0; i < nl.getLength(); i++) {
237: Element elMinOccurs = (Element) nl.item(i);
238: if (elMinOccurs.getChildNodes().getLength() == 1) {
239: Node node = elMinOccurs.getFirstChild();
240: if (node.getNodeType() == Node.TEXT_NODE) {
241: this .m_nMinOccurs = Integer.parseInt(((Text) node)
242: .getNodeValue());
243: }
244: }
245: }
246:
247: nl = elDomain.getElementsByTagNameNS("DAV:", "maxOccurs");
248: for (int i = 0; i < nl.getLength(); i++) {
249: Element elMaxOccurs = (Element) nl.item(i);
250: if (elMaxOccurs.getChildNodes().getLength() == 1) {
251: Node node = elMaxOccurs.getFirstChild();
252: if (node.getNodeType() == Node.TEXT_NODE) {
253: this .m_nMaxOccurs = Integer.parseInt(((Text) node)
254: .getNodeValue());
255: }
256: }
257: }
258: }
259:
260: public String toString() {
261: StringBuffer sBuff = new StringBuffer();
262:
263: sBuff.append("Domain:\n").append("Resourcetype: ").append(
264: this .m_sResourceType).append("\n")
265: .append("MinOccurs: ").append(this .m_nMinOccurs)
266: .append("\n").append("MaxOccurs: ").append(
267: this .m_nMaxOccurs).append("\n");
268:
269: Iterator itor = this .m_aContentTypes.iterator();
270: while (itor.hasNext()) {
271: sBuff.append("Contenttype: ")
272: .append(((String) itor.next())).append("\n");
273: }
274:
275: itor = this .m_aPaths.iterator();
276: while (itor.hasNext()) {
277: sBuff.append("Path: ").append(((String) itor.next()))
278: .append("\n");
279: }
280:
281: return sBuff.toString();
282: }
283:
284: }
|