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.util.Vector;
013:
014: import org.mmbase.module.core.*;
015: import org.mmbase.module.corebuilders.InsRel;
016: import org.mmbase.module.corebuilders.RelDef;
017: import org.mmbase.util.logging.Logger;
018: import org.mmbase.util.logging.Logging;
019: import org.w3c.dom.NamedNodeMap;
020: import org.w3c.dom.Node;
021: import org.xml.sax.InputSource;
022:
023: /**
024: * This class reads a relation node from an exported application.
025: * @application Applications
026: * @move org.mmbase.util.xml
027: * @author Daniel Ockeloen
028: * @author Michiel Meeuwissen
029: * @version $Id: XMLRelationNodeReader.java,v 1.29 2007/02/11 19:21:11 nklasens Exp $
030: */
031: public class XMLRelationNodeReader extends XMLNodeReader {
032:
033: private static final Logger log = Logging
034: .getLoggerInstance(XMLRelationNodeReader.class);
035:
036: /**
037: * @since MMBase-1.8
038: */
039: public XMLRelationNodeReader(InputSource is, ResourceLoader path) {
040: super (is, path);
041: }
042:
043: public Vector<MMObjectNode> getNodes(MMBase mmbase) {
044: Vector<MMObjectNode> nodes = new Vector<MMObjectNode>();
045: Node n1 = document.getFirstChild();
046: if (n1.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
047: n1 = n1.getNextSibling();
048: }
049: while (n1 != null) {
050: MMObjectBuilder bul = mmbase.getMMObject(n1.getNodeName());
051: if (bul == null) {
052: log.error("Can't get builder with name: '"
053: + n1.getNodeName() + "'");
054: } else {
055: Node n2 = n1.getFirstChild();
056: while (n2 != null) {
057: if (n2.getNodeName().equals("node")) {
058: NamedNodeMap nm = n2.getAttributes();
059: if (nm != null) {
060: Node n4 = nm.getNamedItem("owner");
061: MMObjectNode newNode = bul.getNewNode(n4
062: .getNodeValue());
063: try {
064: n4 = nm.getNamedItem("number");
065: int num = Integer.parseInt(n4
066: .getNodeValue());
067: newNode.setValue("number", num);
068:
069: n4 = nm.getNamedItem("snumber");
070: int rnum = Integer.parseInt(n4
071: .getNodeValue());
072: newNode.setValue("snumber", rnum);
073: n4 = nm.getNamedItem("dnumber");
074: int dnum = Integer.parseInt(n4
075: .getNodeValue());
076: newNode.setValue("dnumber", dnum);
077: n4 = nm.getNamedItem("rtype");
078: String rname = n4.getNodeValue();
079: RelDef reldef = mmbase.getRelDef();
080: if (reldef == null) {
081: log
082: .error("XMLRelationReader -> can't get reldef builder");
083: return null;
084: }
085: // figure out rnumber
086: int rnumber = reldef
087: .getNumberByName(rname);
088: newNode.setValue("rnumber", rnumber);
089:
090: // directionality
091:
092: if (InsRel.usesdir) {
093: n4 = nm.getNamedItem("dir");
094: int dir = 0;
095: if (n4 != null) {
096: String dirs = n4.getNodeValue();
097: if ("unidirectional"
098: .equals(dirs)) {
099: dir = 1;
100: } else if ("bidirectional"
101: .equals(dirs)) {
102: dir = 2;
103: } else {
104: log
105: .error("invalid 'dir' attribute encountered in "
106: + bul
107: .getTableName()
108: + " value="
109: + dirs);
110: }
111: }
112: if (dir == 0) {
113: MMObjectNode relnode = reldef
114: .getNode(rnumber);
115: if (relnode != null) {
116: dir = relnode
117: .getIntValue("dir");
118: }
119: }
120: if (dir != 1)
121: dir = 2;
122: newNode.setValue("dir", dir);
123: }
124:
125: } catch (Exception e) {
126: log.error(e);
127: log.error(Logging.stackTrace(e));
128: }
129: Node n5 = n2.getFirstChild();
130: while (n5 != null) {
131: String key = n5.getNodeName();
132: if (n5.getNodeType() == Node.ELEMENT_NODE) {
133: Node n6 = n5.getFirstChild();
134: String value = "";
135: if (n6 != null) {
136: value = n6.getNodeValue(); // needs to be a loop
137: }
138: setValue(bul, newNode, n5, key,
139: value);
140: }
141: n5 = n5.getNextSibling();
142: }
143: nodes.add(newNode);
144: }
145: }
146: n2 = n2.getNextSibling();
147: }
148: }
149: n1 = n1.getNextSibling();
150: }
151: return nodes;
152: }
153: }
|