001: /*
002: * Copyright (c) 2001, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: /**
024: * @author Jacob Smullyan
025: * @version $Revision: 1.5 $
026: */package org.skunk.minixml;
027:
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.ListIterator;
031: import java.util.StringTokenizer;
032:
033: /**
034: * an insultingly simple wrapper for a bunch of document elements.
035: */
036: public class XMLDocument {
037: private ArrayList contents = new ArrayList();
038: private boolean lenient;
039:
040: public XMLDocument() {
041: this (false);
042: }
043:
044: public XMLDocument(boolean lenient) {
045: this .lenient = lenient;
046: }
047:
048: public boolean isLenient() {
049: return this .lenient;
050: }
051:
052: public XMLDocument addElement(Object elem)
053: throws MalformedXMLException {
054: if (elem instanceof XMLElement) {
055: if (hasRoot() && (!lenient))
056: throw new MalformedXMLException(
057: "adding more than one root element in strict parse mode");
058: }
059: contents.add(elem);
060: return this ;
061: }
062:
063: public boolean hasRoot() {
064: int elemCnt = 0;
065: for (ListIterator lit = contents.listIterator(); lit.hasNext();) {
066: if (lit.next() instanceof XMLElement)
067: elemCnt++;
068: if (elemCnt == 2)
069: break;
070: }
071: return (elemCnt == 1);
072: }
073:
074: public ListIterator contents() {
075: return contents.listIterator();
076: }
077:
078: /**
079: * returns the first XMLElement in the contents collection
080: */
081: public XMLElement getRootElement() {
082: for (ListIterator lit = contents.listIterator(); lit.hasNext();) {
083: Object o = lit.next();
084: if (o instanceof XMLElement)
085: return (XMLElement) o;
086: }
087: return null;
088: }
089:
090: /**
091: * convenience method for accessing an element with
092: * a url-like path, e.g., html/body/h1. Always returns
093: * the first matching element.
094: */
095: public XMLElement getElement(String urlLikePath) {
096: StringTokenizer st = new StringTokenizer(urlLikePath, "/");
097: XMLElement elem = getRootElement();
098: if (st.countTokens() < 1)
099: return null;
100: if (!st.nextToken().equals(elem.getElementName()))
101: return null;
102: while (st.hasMoreTokens()) {
103: elem = elem.getChild(st.nextToken());
104: if (elem == null)
105: break;
106: }
107: return elem;
108: }
109:
110: /**
111: * returns a textual representation of the document,
112: * itself xml
113: */
114: public String toString() {
115: StringBuffer sb = new StringBuffer();
116: for (ListIterator li = contents.listIterator(); li.hasNext();)
117: sb.append(li.next());
118: return sb.toString();
119: }
120:
121: /**
122: * convenience method
123: */
124: public final byte[] getBytes() {
125: return toString().getBytes();
126: }
127: }
128:
129: /* $Log: XMLDocument.java,v $
130: /* Revision 1.5 2001/07/17 03:00:04 smulloni
131: /* added XMLCData class to represent CDATA, and fixed CDATA parsing, which was
132: /* wrong in any case; added license preambles where I had forgotten to put them.
133: /*
134: /* Revision 1.4 2000/11/09 23:35:11 smullyan
135: /* log added to every Java file, with the help of python. Lock stealing
136: /* implemented, and treatment of locks made more robust.
137: /* */
|