001: /*
002: * Container.java
003: *
004: *
005: * Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.xml.datamodel;
054:
055: import org.w3c.dom.Attr;
056: import org.w3c.dom.Document;
057: import org.w3c.dom.NamedNodeMap;
058: import org.w3c.dom.Node;
059: import org.w3c.dom.NodeList;
060:
061: import java.util.*;
062:
063: /**
064: * Contains name/value pairs called literals.
065: *
066: * @author Lars Andersen
067: */
068:
069: public class Container extends Object {
070: com.rimfaxe.util.RimfaxeVector simpletags = new com.rimfaxe.util.RimfaxeVector();
071: String name = "Container";
072: HashMap literals = new HashMap();
073: HashMap iterators = new HashMap();
074: HashMap containers = new HashMap();
075:
076: String command = "unknown";
077: String datakey = "0";
078:
079: com.rimfaxe.util.Calendar senddate = new com.rimfaxe.util.Calendar();
080:
081: /**
082: * Creates new Container
083: *
084: * @return
085: */
086: public Container() {
087: }
088:
089: /**
090: * Creates new Container from xml string
091: *
092: * @param xml
093: * @return
094: */
095: public Container(String xml) {
096: simpletags.addElement("br");
097: simpletags.addElement("img");
098: simpletags.addElement("hr");
099: simpletags.addElement("meta");
100: simpletags.addElement("input");
101:
102: StringBuffer doc = new StringBuffer();
103:
104: // !!!!!!!!!!!!!!!!!!!!
105:
106: doc.append("DATA.DTD");
107: doc.append(xml);
108: com.rimfaxe.xml.ParserInterface parser = com.rimfaxe.xml.ParserFactory
109: .getInstance().checkoutParser();
110: parser.parse(xml, false);
111: Document document = parser.getDocument();
112: }
113:
114: /**
115: * Creates new Container from XML DOM
116: * Name is taken from XML DOM (id attribute)
117: *
118: * @param node XML DOM
119: * @return
120: */
121: public Container(Node node) {
122: simpletags.addElement("br");
123: simpletags.addElement("img");
124: simpletags.addElement("hr");
125: simpletags.addElement("meta");
126: simpletags.addElement("input");
127: NodeList children = node.getChildNodes();
128: if (children != null) {
129: NamedNodeMap attrs = node.getAttributes();
130:
131: if (attrs.getNamedItem("id") != null) {
132: String id = attrs.getNamedItem("id").getNodeValue();
133: this .name = id;
134: }
135:
136: int len = children.getLength();
137: for (int i = 0; i < len; i++) {
138: traverse(children.item(i));
139: }
140: }
141: }
142:
143: /**
144: * Creates new Container
145: *
146: * @param name name of Container
147: * @param node XML DOM
148: * @return
149: */
150: public Container(String name, Node node) {
151: this .name = name;
152: NodeList children = node.getChildNodes();
153: if (children != null) {
154: int len = children.getLength();
155: for (int i = 0; i < len; i++) {
156: traverse(children.item(i));
157: }
158: }
159: }
160:
161: /**
162: * Set name of Container
163: *
164: * @param val name of Container
165: */
166: public void setName(String val) {
167: this .name = val;
168: }
169:
170: /**
171: * Get name of Container
172: *
173: * @return name of Container
174: */
175: public String getName() {
176: return this .name;
177: }
178:
179: /**
180: * Add a literal object to Container
181: *
182: * @param key
183: * @param val
184: */
185: public void addObject(String key, Object val) {
186: literals.put(key.toLowerCase(), val);
187: }
188:
189: /**
190: * Add a literal String to Container
191: *
192: * @param key
193: * @param val
194: */
195: public void addLiteral(String key, String val) {
196: literals.put(key.toLowerCase(), val);
197: }
198:
199: /**
200: * Add an iterator to Container
201: *
202: * @param key
203: * @param iterator
204: */
205: public void addIterator(String key, Iterator iterator) {
206: iterators.put(key.toLowerCase(), iterator);
207: }
208:
209: /**
210: * Add a Container to Container
211: *
212: * @param key
213: * @param container
214: */
215: public void addContainer(String key, Container container) {
216: containers.put(key.toLowerCase(), container);
217: }
218:
219: /**
220: * Get Object from Container
221: *
222: * @param key
223: * @return
224: */
225: public Object getObject(String key) {
226: return literals.get(key.toLowerCase());
227: }
228:
229: /**
230: * Get String Literal from Container
231: *
232: * @param key
233: * @return
234: */
235: public String getLiteral(String key) {
236: String res = (String) literals.get(key.toLowerCase());
237: if (res == null)
238: res = "";
239: return res;
240: }
241:
242: /**
243: * Get Iterator from Container
244: *
245: * @param key
246: * @return
247: */
248: public Iterator getIterator(String key) {
249: Iterator res = (Iterator) iterators.get(key.toLowerCase());
250: return res;
251: }
252:
253: /**
254: * Get Container from Container
255: *
256: * @param key
257: * @return
258: */
259: public Container getContainer(String key) {
260: Container res = (Container) containers.get(key.toLowerCase());
261: return res;
262: }
263:
264: /**
265: * Traverse Container and add elements to container
266: * Possible elements are:
267: * value -> adds literal with key = attribute("key") and value = children
268: * command -> sets command field
269: * senddate -> sets senddata field
270: * datakey -> sets datakey field
271: * subsection -> adds container
272: * list -> adds iterator
273: *
274: * @param node
275: * @return
276: */
277: private String traverse(Node node) {
278:
279: StringBuffer str = new StringBuffer();
280:
281: if (node == null) {
282:
283: return "";
284: }
285: int type = node.getNodeType();
286: switch (type) {
287:
288: case Node.DOCUMENT_NODE: {
289: traverse(((Document) node).getDocumentElement());
290: break;
291: }
292:
293: case Node.ELEMENT_NODE: {
294: if (node.getNodeName().equalsIgnoreCase("value")) {
295: NodeList children = node.getChildNodes();
296: if (children != null) {
297: NamedNodeMap attrs = node.getAttributes();
298: String id = attrs.getNamedItem("id").getNodeValue();
299: int len = children.getLength();
300: StringBuffer buf = new StringBuffer();
301: for (int i = 0; i < len; i++) {
302: buf.append(traverse(children.item(i)));
303: }
304: addLiteral(id.toLowerCase(), "" + buf);
305: }
306: } else if (node.getNodeName().equalsIgnoreCase("command")) {
307: NodeList children = node.getChildNodes();
308: if (children != null) {
309: int len = children.getLength();
310: for (int i = 0; i < len; i++) {
311: String val = traverse(children.item(i));
312: command = val;
313: }
314: }
315: } else if (node.getNodeName().equalsIgnoreCase("senddate")) {
316: NodeList children = node.getChildNodes();
317: if (children != null) {
318: int len = children.getLength();
319: for (int i = 0; i < len; i++) {
320: String val = traverse(children.item(i));
321: senddate = new com.rimfaxe.util.Calendar(val);
322: }
323: }
324: } else if (node.getNodeName().equalsIgnoreCase("datakey")) {
325: NodeList children = node.getChildNodes();
326: if (children != null) {
327: int len = children.getLength();
328: for (int i = 0; i < len; i++) {
329: String val = traverse(children.item(i));
330: datakey = val;
331: }
332: }
333: } else if (node.getNodeName()
334: .equalsIgnoreCase("subsection")) {
335: NamedNodeMap attrs = node.getAttributes();
336: String id = attrs.getNamedItem("id").getNodeValue();
337: addContainer(id.toLowerCase(), new Container(node));
338: } else if (node.getNodeName().equalsIgnoreCase("list")) {
339: NamedNodeMap attrs = node.getAttributes();
340: String id = attrs.getNamedItem("id").getNodeValue();
341: addIterator(id.toLowerCase(), new Iterator(node));
342: }
343:
344: else if (node.getNodeName().equalsIgnoreCase("entity")) {
345: String id = node.getAttributes().getNamedItem("id")
346: .getNodeValue();
347: return "&" + id + ";";
348: }
349:
350: else {
351: StringBuffer buf = new StringBuffer();
352: NodeList children = node.getChildNodes();
353: if (children != null) { // body tag
354: int len = children.getLength();
355:
356: for (int i = 0; i < len; i++) {
357: buf.append(traverse(children.item(i)));
358: }
359:
360: //return genericBodyTag( node.getNodeName(), node.getAttributes(), ""+buf );
361: } else { // simple tag
362: //return genericSimpleTag( node.getNodeName(), node.getAttributes());
363: }
364: return genericTag(node.getNodeName(), node
365: .getAttributes(), "" + buf);
366: }
367: break;
368: }
369:
370: case Node.ENTITY_REFERENCE_NODE: {
371: //System.out.println("ENTITY_REFERENCE_NODE -> name="+node.getNodeName()+" value="+node.getNodeValue());
372: str.append("&" + node.getNodeName() + ";");
373: break;
374: }
375:
376: case Node.TEXT_NODE: {
377: //if (node instanceof TextImpl)
378: //{
379: str.append(node.getNodeValue());
380: //}
381: break;
382: }
383: }
384: return str.toString();
385: }
386:
387: /**
388: * Get container as XML
389: *
390: * @return
391: */
392: public String toXML() {
393: StringBuffer buf = new StringBuffer();
394: buf.append("<subsection id=\"" + name + "\">\n");
395:
396: java.util.Iterator iter = literals.keySet().iterator();
397: while (iter.hasNext()) {
398: String hkey = "" + iter.next();
399: buf.append(" <value id=\"" + hkey + "\">"
400: + literals.get(hkey) + "</value>\n");
401: }
402:
403: iter = containers.keySet().iterator();
404: while (iter.hasNext()) {
405: Container c = (Container) containers.get(iter.next());
406: buf.append(" " + c.toXML());
407: }
408:
409: iter = iterators.keySet().iterator();
410: while (iter.hasNext()) {
411: Iterator i = (Iterator) iterators.get(iter.next());
412: buf.append(" " + i.toXML());
413: }
414:
415: buf.append("</subsection>\n");
416: return "" + buf;
417: }
418:
419: public String genericTag(String name, NamedNodeMap attrs,
420: String body) {
421: StringBuffer att = new StringBuffer();
422: att.append("<" + name);
423:
424: for (int i = 0; i < attrs.getLength(); i++) {
425: Node node = attrs.item(i);
426: att.append(" " + node.getNodeName() + "=\""
427: + node.getNodeValue() + "\"");
428: }
429:
430: if (simpletags.contains(name)) {
431: att.append("/>");
432: } else {
433: att.append(">");
434: att.append(body);
435: att.append("</" + name + ">\n");
436: }
437: return att.toString();
438: }
439:
440: public String genericBodyTag(String name, NamedNodeMap attrs,
441: String body) {
442: //if (simpletags.contains(name)) return genericSimpleTag(name,attrs);
443:
444: StringBuffer att = new StringBuffer();
445: att.append("<" + name);
446:
447: for (int i = 0; i < attrs.getLength(); i++) {
448: Node node = attrs.item(i);
449: att.append(" " + node.getNodeName() + "=\""
450: + node.getNodeValue() + "\"");
451: }
452:
453: att.append(">");
454: att.append(body);
455: att.append("</" + name + ">\n");
456: return att.toString();
457: }
458:
459: public String genericSimpleTag(String name, NamedNodeMap attrs) {
460: StringBuffer att = new StringBuffer();
461: att.append("<" + name);
462:
463: for (int i = 0; i < attrs.getLength(); i++) {
464: Node node = attrs.item(i);
465: att.append(" " + node.getNodeName() + "=\""
466: + node.getNodeValue() + "\"");
467: }
468:
469: att.append("/>");
470:
471: return att.toString();
472: }
473: }
|