KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > util > DocNodeImporter


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.jcr.impl.util;
7
8 import org.exoplatform.services.jcr.impl.core.NodeImpl;
9 import org.xml.sax.*;
10
11 import javax.jcr.StringValue;
12 import javax.xml.parsers.SAXParserFactory JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Stack JavaDoc;
18
19 /**
20  * Created by The eXo Platform SARL .
21  *
22  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
23  * @version $Id: DocNodeImporter.java,v 1.6 2004/08/18 23:07:00 benjmestrallet Exp $
24  */

25
26 // WARNING: Experimantal implementation
27
public class DocNodeImporter implements ContentHandler {
28
29   Stack JavaDoc tree;
30   String JavaDoc[] uris;
31
32   private DocNodeImporter(NodeImpl parent, String JavaDoc[] URIs) {
33     tree = new Stack JavaDoc();
34     tree.push(parent);
35     uris = URIs;
36   }
37
38   private boolean isAllowedUri(String JavaDoc uri) {
39     for (int i = 0; i < uris.length; i++)
40       if (uris[i].equals(uri))
41         return true;
42     return false;
43   }
44
45   public static void fillNode(NodeImpl parent, InputStream JavaDoc stream, String JavaDoc[] nsURIs)
46       throws IOException JavaDoc, SAXException {
47     DocNodeImporter importer = new DocNodeImporter(parent, nsURIs);
48     XMLReader reader;
49     try {
50       SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
51       factory.setNamespaceAware(true);
52       reader = factory.newSAXParser().getXMLReader();
53
54     } catch (Exception JavaDoc e) {
55       throw new SAXException(e.getMessage());
56     }
57     reader.setContentHandler(importer);
58     reader.parse(new InputSource(stream));
59   }
60
61   public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts)
62       throws SAXException {
63     //TODO manage UUID
64
//TODO manage auto created properties
65
NodeImpl parent = (NodeImpl) tree.peek();
66     if("root".equals(qName))
67       return;
68     NodeImpl node;
69     HashMap JavaDoc props = new HashMap JavaDoc();
70     String JavaDoc nodeType = "nt:default";
71
72     if (atts != null) {
73       int length = atts.getLength();
74       for (int i = 0; i < length; i++) {
75         String JavaDoc propName;
76         String JavaDoc attrQName = atts.getQName(i);
77         if (!"".equals(attrQName))
78           propName = attrQName;
79         else
80           propName = atts.getLocalName(i);
81
82         if (propName.equals("jcr:primaryType"))
83           nodeType = atts.getValue(i);
84         else
85           props.put(propName, atts.getValue(i));
86       }
87     }
88
89     try {
90       node = (NodeImpl) parent.addNode(qName, nodeType);
91       Iterator JavaDoc keys = props.keySet().iterator();
92       while (keys.hasNext()) {
93         String JavaDoc key = (String JavaDoc) keys.next();
94         String JavaDoc value = (String JavaDoc) props.get(key);
95         node.setProperty(key, new StringValue(value));
96       }
97     } catch (Exception JavaDoc e) {
98       throw new SAXException(e.getMessage(), e);
99     }
100     tree.push(node);
101   }
102
103   public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException {
104     tree.pop();
105   }
106
107   public void characters(char[] ch, int start, int length) throws SAXException {
108   }
109
110   public void setDocumentLocator(Locator locator) {
111   }
112
113   public void startDocument() throws SAXException {
114   }
115
116   public void endDocument() throws SAXException {
117   }
118
119   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException {
120   }
121
122   public void endPrefixMapping(String JavaDoc prefix) throws SAXException {
123   }
124
125   public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
126   }
127
128   public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException {
129   }
130
131   public void skippedEntity(String JavaDoc name) throws SAXException {
132   }
133 }
134
Popular Tags