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.util;
011:
012: import java.io.*;
013: import java.util.Locale;
014: import java.util.Set;
015: import java.util.Vector;
016:
017: import org.mmbase.bridge.Field;
018: import org.mmbase.module.core.*;
019: import org.mmbase.util.logging.Logger;
020: import org.mmbase.util.logging.Logging;
021: import org.mmbase.util.xml.DocumentReader;
022: import org.w3c.dom.*;
023: import org.xml.sax.InputSource;
024:
025: /**
026: * This class reads a node from an exported application.
027: * @application Applications
028: * @move org.mmbase.util.xml
029: * @author Daniel Ockeloen
030: * @author Michiel Meeuwissen
031: * @version $Id: XMLNodeReader.java,v 1.48 2007/06/21 15:50:22 nklasens Exp $
032: */
033: public class XMLNodeReader extends DocumentReader {
034: private static final Logger log = Logging
035: .getLoggerInstance(XMLNodeReader.class);
036:
037: private ResourceLoader path;
038:
039: public boolean loadBinaries = true;
040:
041: /**
042: * @since MMBase-1.8
043: */
044: public XMLNodeReader(InputSource is, ResourceLoader path) {
045: super (is, false);
046: this .path = path;
047: }
048:
049: /**
050: * get the name of this application
051: */
052: public String getExportSource() {
053: Node n1 = document.getDocumentElement();
054: if (n1 != null) {
055: NamedNodeMap nm = n1.getAttributes();
056: if (nm != null) {
057: Node n2 = nm.getNamedItem("exportsource");
058: return (n2.getNodeValue());
059: }
060: } else {
061: log.warn("exportsource attribute missing");
062: }
063: return null;
064: }
065:
066: /**
067: * get the name of this application
068: */
069: public int getTimeStamp() {
070: Node n1 = document.getFirstChild();
071: if (n1.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
072: n1 = n1.getNextSibling();
073: }
074: if (n1 != null) {
075: NamedNodeMap nm = n1.getAttributes();
076: if (nm != null) {
077: Node n2 = nm.getNamedItem("timestamp");
078: try {
079: java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
080: "yyyyMMddhhmmss", Locale.US);
081: int times = (int) (formatter.parse(
082: n2.getNodeValue()).getTime() / 1000);
083: //int times=DateSupport.parsedatetime(n2.getNodeValue());
084: return times;
085: } catch (java.text.ParseException e) {
086: return -1;
087: }
088: }
089: } else {
090: log.warn("timestamp attribute missing");
091: }
092: return -1;
093: }
094:
095: public Vector<MMObjectNode> getNodes(MMBase mmbase) {
096: Vector<MMObjectNode> nodes = new Vector<MMObjectNode>();
097: Node n1 = document.getDocumentElement();
098: while (n1 != null) {
099: MMObjectBuilder bul = mmbase.getMMObject(n1.getNodeName());
100: if (bul == null) {
101: log.error("Can't get builder with name: '"
102: + n1.getNodeName() + "'");
103: } else {
104: Node n2 = n1.getFirstChild();
105: while (n2 != null) {
106: if (n2.getNodeName().equals("node")) {
107: NamedNodeMap nm = n2.getAttributes();
108: if (nm != null) {
109: Node n4 = nm.getNamedItem("owner");
110: MMObjectNode newNode = null;
111: if (n4 != null) {
112: newNode = bul.getNewNode(n4
113: .getNodeValue());
114: } else {
115: newNode = bul.getNewNode("import");
116: }
117: n4 = nm.getNamedItem("alias");
118: if (n4 != null) {
119: // tokenize here!
120: String n4value = n4.getNodeValue();
121: String[] aliases = n4value.split(",");
122: for (String alias : aliases) {
123: log.info("Setting alias to "
124: + alias);
125: newNode.setAlias(alias);
126: }
127: }
128: n4 = nm.getNamedItem("number");
129: try {
130: int num = Integer.parseInt(n4
131: .getNodeValue());
132: newNode.setValue("number", num);
133: } catch (Exception e) {
134: }
135: Node n5 = n2.getFirstChild();
136: while (n5 != null) {
137: if (n5.getNodeType() == Node.ELEMENT_NODE) {
138: String key = n5.getNodeName();
139: NodeList nl = n5.getChildNodes();
140: StringBuffer res = new StringBuffer(
141: "");
142: for (int i = 0; i < nl.getLength(); i++) {
143: Node n = nl.item(i);
144: if ((n.getNodeType() == Node.TEXT_NODE)
145: || (n.getNodeType() == Node.CDATA_SECTION_NODE)) {
146: res.append(n.getNodeValue()
147: .trim());
148: }
149: }
150: String value = res.toString();
151:
152: setValue(bul, newNode, n5, key,
153: value);
154: }
155: n5 = n5.getNextSibling();
156: }
157: nodes.add(newNode);
158: }
159: }
160: n2 = n2.getNextSibling();
161: }
162: }
163: n1 = n1.getNextSibling();
164: }
165: return nodes;
166: }
167:
168: protected void setValue(MMObjectBuilder bul, MMObjectNode newNode,
169: Node n5, String key, String value) {
170: int type = bul.getDBType(key);
171: if (type != -1) {
172: if (type == Field.TYPE_STRING || type == Field.TYPE_XML) {
173: if (value == null) {
174: value = "";
175: }
176: newNode.setValue(key, value);
177: } else if (type == Field.TYPE_NODE) {
178: // do not really set it, because we need syncnodes later for this.
179: newNode.storeValue("__" + key, value); // yes, this is hackery, I'm sorry.
180: newNode.setValue(key, null);
181: } else if (type == Field.TYPE_INTEGER) {
182: try {
183: newNode.setValue(key, Integer.parseInt(value));
184: } catch (Exception e) {
185: log.warn("error setting integer-field '" + key
186: + "' to '" + value + "' because " + e);
187: newNode.setValue(key, -1);
188: }
189: } else if (type == Field.TYPE_FLOAT) {
190: try {
191: newNode.setValue(key, Float.parseFloat(value));
192: } catch (Exception e) {
193: log.warn("error setting float-field '" + key
194: + "' to '" + value + "' because " + e);
195: newNode.setValue(key, -1);
196: }
197: } else if (type == Field.TYPE_DOUBLE) {
198: try {
199: newNode.setValue(key, Double.parseDouble(value));
200: } catch (Exception e) {
201: log.warn("error setting double-field '" + key
202: + "' to '" + value + "' because " + e);
203: newNode.setValue(key, -1);
204: }
205: } else if (type == Field.TYPE_LONG) {
206: try {
207: newNode.setValue(key, Long.parseLong(value));
208: } catch (Exception e) {
209: log.warn("error setting long-field '" + key
210: + "' to '" + value + "' because " + e);
211: newNode.setValue(key, -1);
212: }
213: } else if (type == Field.TYPE_DATETIME) {
214: newNode.setValue(key, Casting.toDate(value));
215: } else if (type == Field.TYPE_BOOLEAN) {
216: newNode.setValue(key, Casting.toBoolean(value));
217: } else if (type == Field.TYPE_BINARY) {
218: NamedNodeMap nm2 = n5.getAttributes();
219: Node n7 = nm2.getNamedItem("file");
220: try {
221: if (loadBinaries) {
222: newNode.setValue(key, readBytesStream(n7
223: .getNodeValue()));
224: } else {
225: newNode.setValue(key, n7.getNodeValue());
226: }
227: } catch (IOException ioe) {
228: log.warn("Could not set field " + key + " " + ioe);
229: }
230: } else {
231: log.error("CoreField not found for #" + type
232: + " was not known for field with name: '" + key
233: + "' and with value: '" + value + "'");
234: }
235: }
236: }
237:
238: private byte[] readBytesStream(String resourceName)
239: throws IOException {
240: InputStream stream = path.getResourceAsStream(resourceName);
241: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
242: int c = stream.read();
243: while (c != -1) {
244: buffer.write(c);
245: c = stream.read();
246: }
247: return buffer.toByteArray();
248: }
249:
250: public void loadBinairyFields(MMObjectNode newNode) {
251: Set<String> fieldNames = newNode.getBuilder().getFieldNames();
252: if (fieldNames != null && fieldNames.size() > 0) {
253: for (String fieldName : fieldNames) {
254: int fieldDBType = newNode.getBuilder().getDBType(
255: fieldName);
256: if (fieldDBType == Field.TYPE_BINARY) {
257: try {
258: String resource = newNode
259: .getStringValue(fieldName);
260: newNode.setValue(fieldName,
261: readBytesStream(resource));
262: } catch (Exception setValueEx) {
263: log.error(setValueEx);
264: }
265: }
266: }
267: }
268: }
269:
270: public void setLoadBinaries(boolean loadBinaries) {
271: this.loadBinaries = loadBinaries;
272: }
273: }
|