001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module.builders;
011:
012: import java.io.File;
013: import java.io.IOException;
014: import java.util.*;
015:
016: import javax.xml.parsers.DocumentBuilder;
017:
018: import org.mmbase.module.core.MMObjectNode;
019: import org.mmbase.util.logging.*;
020: import org.mmbase.util.xml.DocumentReader;
021:
022: import org.w3c.dom.*;
023: import org.xml.sax.SAXException;
024:
025: /**
026: * @javadoc
027: * @deprecated is this (cacheversionfile) used? seems obsolete now
028: * @author Daniel Ockeloen
029: * @version $Id: VersionXMLCacheNodeReader.java,v 1.9 2007/02/11 19:21:12 nklasens Exp $
030: */
031: public class VersionXMLCacheNodeReader {
032:
033: private static Logger log = Logging
034: .getLoggerInstance(VersionCacheNode.class.getName());
035: Document document;
036: Versions parent;
037:
038: public VersionXMLCacheNodeReader(String filename) {
039: try {
040: DocumentBuilder db = DocumentReader
041: .getDocumentBuilder(false);
042: File file = new File(filename);
043: if (!file.exists()) {
044: log.error("no cache version " + filename + " found)");
045: }
046: document = db.parse(file);
047: } catch (SAXException e) {
048: log.error("", e);
049: } catch (IOException e) {
050: log.error("", e);
051: }
052: }
053:
054: public void setBuilder(Versions parent) {
055: this .parent = parent;
056: }
057:
058: /**
059: */
060: public Hashtable<String, Vector<VersionCacheNode>> getCacheVersions(
061: Hashtable<String, Vector<VersionCacheNode>> handlers) {
062: Node n1 = document.getFirstChild();
063: if (n1.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
064: n1 = n1.getNextSibling();
065: }
066: if (n1 != null) {
067: Node n2 = n1.getFirstChild();
068: while (n2 != null) {
069: if (n2.getNodeName().equals("cacheversion")) {
070:
071: // decode all the needed values in the replace itself
072: NamedNodeMap nm = n2.getAttributes();
073: if (nm != null) {
074: Node n3 = nm.getNamedItem("name");
075: if (n3 != null) {
076: String name = n3.getNodeValue();
077: if (log.isDebugEnabled())
078: log.debug("name = " + name);
079: // find the node
080: MMObjectNode versionnode = null;
081: String query = "name=='" + name
082: + "'+type=='cache'";
083: Enumeration<MMObjectNode> b = parent
084: .search(query);
085: if (b.hasMoreElements()) {
086: versionnode = b.nextElement();
087: }
088: if (log.isDebugEnabled())
089: log.debug("versionnode=" + versionnode);
090: VersionCacheNode cnode = new VersionCacheNode(
091: parent.getMMBase());
092: cnode.setVersionNode(versionnode);
093:
094: n3 = n2.getFirstChild();
095: while (n3 != null) {
096:
097: if (n3.getNodeName().equals("when")) {
098: // create new when node
099: VersionCacheWhenNode wn = new VersionCacheWhenNode();
100: nm = n3.getAttributes();
101: if (nm != null) {
102: Node n5 = nm
103: .getNamedItem("type");
104: if (n5 != null) {
105: String types = n5
106: .getNodeValue();
107: StringTokenizer tok = new StringTokenizer(
108: types, ",\n\r");
109: while (tok.hasMoreTokens()) {
110: String type = tok
111: .nextToken();
112: wn.addType(type);
113:
114: // place ourselfs in the call hash
115:
116: Vector<VersionCacheNode> subs = handlers
117: .get(type);
118: if (subs == null) {
119: subs = new Vector<VersionCacheNode>();
120: subs
121: .addElement(cnode);
122: handlers.put(type,
123: subs);
124: } else {
125: subs
126: .addElement(cnode);
127: }
128: }
129: }
130: n5 = nm.getNamedItem("node");
131: if (n5 != null) {
132: String nodes = n5
133: .getNodeValue();
134: StringTokenizer tok = new StringTokenizer(
135: nodes, ",\n\r");
136: while (tok.hasMoreTokens()) {
137: String type = tok
138: .nextToken();
139: wn.addNode(type);
140: }
141: }
142: }
143: cnode.addWhen(wn);
144: }
145: n3 = n3.getNextSibling();
146: }
147: }
148: }
149: }
150: n2 = n2.getNextSibling();
151: }
152: }
153: return handlers;
154: }
155:
156: public Vector<Map<String, String>> getDefines() {
157: Vector<Map<String, String>> results = new Vector<Map<String, String>>();
158: Node n1 = document.getFirstChild();
159: if (n1 != null) {
160: Node n2 = n1.getFirstChild();
161: while (n2 != null) {
162: if (n2.getNodeName().equals("define")) {
163: Map<String, String> rep = new Hashtable<String, String>();
164:
165: // decode all the needed values in the replace itself
166: NamedNodeMap nm = n2.getAttributes();
167: if (nm != null) {
168: Node n3 = nm.getNamedItem("type");
169: if (n3 != null) {
170: rep.put("type", n3.getNodeValue());
171: }
172: n3 = nm.getNamedItem("id");
173: if (n3 != null) {
174: rep.put("id", n3.getNodeValue());
175: }
176:
177: n3 = nm.getNamedItem("width");
178: if (n3 != null) {
179: rep.put("width", n3.getNodeValue());
180: }
181:
182: n3 = nm.getNamedItem("height");
183: if (n3 != null) {
184: rep.put("height", n3.getNodeValue());
185: }
186:
187: n3 = nm.getNamedItem("src");
188: if (n3 != null) {
189: rep.put("src", n3.getNodeValue());
190: }
191:
192: n3 = nm.getNamedItem("value");
193: if (n3 != null) {
194: rep.put("value", n3.getNodeValue());
195: }
196: n3 = nm.getNamedItem("file");
197: if (n3 != null) {
198: rep.put("valuefile", n3.getNodeValue());
199: }
200:
201: }
202:
203: results.addElement(rep);
204: }
205: n2 = n2.getNextSibling();
206: }
207: }
208: return results;
209: }
210:
211: }
|