KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > richtext > Mmxf


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.richtext;
12
13 import org.w3c.dom.*;
14 import javax.xml.parsers.DocumentBuilder JavaDoc;
15 import org.mmbase.bridge.*;
16 import org.mmbase.util.ResourceLoader;
17 import java.io.*;
18
19 import org.mmbase.util.logging.*;
20
21 /**
22  * Utilities related to the 'mmxf' rich field format of MMBase and bridge.
23  *
24  * @author Michiel Meeuwissen
25  * @version $Id: Mmxf.java,v 1.1 2005/10/25 22:29:17 michiel Exp $
26  * @see org.mmbase.util.transformers.XmlField
27  * @since MMBase-1.8
28  */

29 public class Mmxf {
30
31     private static final Logger log = Logging.getLoggerInstance(Mmxf.class);
32
33     public final static String JavaDoc NAMESPACE = "http://www.mmbase.org/xmlns/mmxf";
34     public final static String JavaDoc DOCUMENTTYPE_PUBLIC = "-//MMBase//DTD mmxf 1.1//EN";
35     public final static String JavaDoc DOCUMENTTYPE_SYSTEM = "http://www.mmbase.org/dtd/mmxf_1_1.dtd";
36
37
38     /**
39      * Defaulting version of {@link #createTree(org.w3c.dom.Node, org.mmbase.bridge.Node, RelationManager, int, String, String, Writer)}.
40      */

41     public static org.mmbase.bridge.Node createTree(org.w3c.dom.Node JavaDoc node, RelationManager relationManager, int depth, Writer buf) {
42         return createTree(node, null, relationManager, depth, "title", "body", buf);
43     }
44
45
46     protected static void exception(Writer buf, String JavaDoc message) {
47         if (buf == null) {
48             throw new IllegalArgumentException JavaDoc(message);
49         } else {
50             try {
51                 buf.write("ERROR: " + message + '\n');
52             } catch (IOException ioe) {
53                 IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(ioe.getMessage() + message);
54                 e.initCause(ioe);
55                 throw e;
56             }
57         }
58     }
59
60     protected static void debug(Writer buf, String JavaDoc message) {
61         if (buf == null) {
62             log.debug(message);
63         } else {
64             try {
65                 buf.write(message + '\n');
66             } catch (IOException ioe) {
67                 IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(ioe.getMessage() + message);
68                 e.initCause(ioe);
69                 throw e;
70             }
71         }
72     }
73     /**
74      * Creates a a tree of Nodes from an mmxf DOM-Node. The (mmxf) document can of course be stored
75      * in one MMXF field, but if it is large, you want to split it up in a tree of nodes. This can
76      * be necessary in an import, where you would e.g. receive a complete book (or chapter) in one
77      * XML document. You could e.g. (XSL) transform it to one huge MMXF and then feed it into this function. See also
78      * {@link org.mmbase.util.transformers.XmlField} for creating MMXF from ASCII.
79      *
80      * If your MMXF is a Document, you must feed {@link org.w3c.dom.Document#getDocumentElement()}
81      *
82      * @param node Source DOM-Node (it's node-name must be 'mmxf' or 'section').
83      * @param root Root node (to be used in index relations) Can be <code>null</code> in which case it will be set equal to the first node created.
84      * @param relationManager Describes the object model in which it must be dispatched. Source and
85      * Destination must be of the same type, the relation must have a field 'pos'.
86      * @param depth How far to dispatch. If -1 then it will be split up until no sections are
87      * remaining.
88      * @param titleField The h-tag of the sections are written to this field.
89      * @param xmlField A new mmxf-document is created for the test and written to this field.
90      * @param feedBack A string buffer for feedback (can be e.g. used for logging or presenting on import-jsp).
91      */

92     public static org.mmbase.bridge.Node createTree(org.w3c.dom.Node JavaDoc node,
93                                                     org.mmbase.bridge.Node root, RelationManager relationManager, int depth, String JavaDoc titleField, String JavaDoc xmlField, Writer feedBack) {
94         String JavaDoc nodeName = node.getNodeName();
95         if (! (nodeName.equals("section") || nodeName.equals("mmxf"))) {
96             exception(feedBack, "dom-Node must be a 'section' or 'mmxf' (but is a " + node.getNodeName() + ")");
97             return null;
98         }
99         NodeManager nm = relationManager.getDestinationManager();
100         org.w3c.dom.NodeList JavaDoc childs = node.getChildNodes();
101         debug(feedBack, "Found " + childs.getLength() + " childs");
102         int i = 0;
103
104         debug(feedBack, "Importing " + nodeName);
105         String JavaDoc title;
106         if (nodeName.equals("section")) {
107             if (childs.getLength() < 1) {
108                 exception(feedBack, "No child nodes! (should at least be a h-child)");
109             }
110             org.w3c.dom.Node JavaDoc h = childs.item(i);
111             if (! h.getNodeName().equals("h")) {
112                 exception(feedBack, "No h-tag");
113             }
114             title = org.mmbase.util.xml.DocumentReader.getNodeTextValue(h);
115             i++;
116         } else {
117             title = "Imported MMXF";
118         }
119         debug(feedBack, "Creating node with title '" + title + "'");
120         Document mmxf = createMmxfDocument();
121         while (i < childs.getLength()) {
122             org.w3c.dom.Node JavaDoc next = childs.item(i);
123             String JavaDoc name = next.getNodeName();
124             if (name.equals("p") || name.equals("table") ||
125                 (depth == 0 && name.equals("section"))) {
126                 org.w3c.dom.Node JavaDoc n = mmxf.importNode(next, true);
127                 debug(feedBack, "appending " + name);
128                 mmxf.getDocumentElement().appendChild(n);
129             } else {
130                 debug(feedBack, "name is not p or table, but " + name + " breaking");
131                 break;
132             }
133             i++;
134         }
135         debug(feedBack, "# handled childs:" + i);
136         // create the node.
137
org.mmbase.bridge.Node newNode = nm.createNode();
138         newNode.setStringValue(titleField, title);
139         newNode.setXMLValue(xmlField, mmxf);
140         newNode.commit();
141         debug(feedBack, "created node " + newNode.getNumber());
142
143         if (root == null) root = newNode;
144
145         int pos = 1;
146         while (i < childs.getLength()) {
147             org.w3c.dom.Node JavaDoc next = childs.item(i);
148             String JavaDoc name = next.getNodeName();
149             debug(feedBack, "found for " + i + " " + name);
150             if (name.equals("section")) {
151                 org.mmbase.bridge.Node destination = createTree(next, root, relationManager, depth > 0 ? depth -1 : depth, titleField, xmlField, feedBack);
152                 Relation relation = relationManager.createRelation(newNode, destination);
153                 relation.setIntValue("pos", pos);
154                 relation.setNodeValue("root", root);
155                 relation.commit();
156                 debug(feedBack, "Created relation " + newNode.getNumber() + " --" + pos + " -->" + destination.getNumber());
157                 pos++;
158             } else {
159                 exception(feedBack, "Not a section, but a " + name);
160             }
161             i++;
162
163         }
164         debug(feedBack, "found " + pos + " subsections on node " + newNode.getNumber());
165         return newNode;
166             
167     }
168
169     /**
170      * Creates an (empty) Mmxf Document
171      */

172     public static Document createMmxfDocument() {
173         DocumentBuilder JavaDoc documentBuilder = org.mmbase.util.xml.DocumentReader.getDocumentBuilder();
174         DOMImplementation impl = documentBuilder.getDOMImplementation();
175         Document document = impl.createDocument(NAMESPACE,
176                                                 "mmxf",
177                                                 impl.createDocumentType("mmxf", DOCUMENTTYPE_PUBLIC, DOCUMENTTYPE_SYSTEM)
178                                                 );
179         document.getDocumentElement().setAttribute("version", "1.1");
180         return document;
181     }
182
183
184     /**
185      * main for testing purposes.
186      */

187     public static void main(String JavaDoc[] argv) {
188         try {
189             CloudContext cc = ContextProvider.getDefaultCloudContext();
190             Cloud cloud = cc.getCloud("mmbase", "class", null);
191             
192             if (argv.length == 0) {
193                 System.out.println("Usage:\n java " + Mmxf.class.getName() + " <-Dmmbase.defaultcloudcontext=rmi://...> <fileName>");
194                 return;
195             }
196             ResourceLoader rc = ResourceLoader.getSystemRoot();
197             
198             System.out.println("" + rc);
199             Document doc = rc.getDocument(argv[0]);
200             
201             System.out.println("Found cloud " + cloud.getUser().getIdentifier());
202             RelationManager relationManager = cloud.getRelationManager("segments", "segments", "index");
203             Writer writer = new BufferedWriter(new OutputStreamWriter(System.out));
204             org.mmbase.bridge.Node node = Mmxf.createTree(doc.getDocumentElement(), relationManager, 3, writer);
205             writer.flush();
206             System.out.println("Created node " + node.getNumber());
207             
208             
209         } catch (Exception JavaDoc e) {
210             System.err.println("" + e);
211         }
212     }
213 }
214
Popular Tags