KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.sax.*;
9 import org.apache.commons.codec.binary.Base64;
10 import org.exoplatform.services.jcr.impl.core.NodeImpl;
11
12 import javax.jcr.*;
13 import javax.xml.parsers.SAXParserFactory JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.ByteArrayInputStream 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: SysNodeImporter.java,v 1.8 2004/08/23 15:28:47 benjmestrallet Exp $
24  */

25
26 public class SysNodeImporter implements ContentHandler {
27
28   private Stack JavaDoc tree;
29   private String JavaDoc propertyName;
30   private int propertyType;
31   private NodeImpl currentNode;
32
33
34   private SysNodeImporter(String JavaDoc rootPath, Ticket ticket) throws RepositoryException {
35     tree = new Stack JavaDoc();
36     tree.push(ticket.getNodeByAbsPath(rootPath));
37   }
38
39   public static void fillItems(String JavaDoc rootPath, InputStream JavaDoc stream, Ticket ticket)
40       throws IOException JavaDoc, SAXException, RepositoryException {
41     SysNodeImporter importer = new SysNodeImporter(rootPath, ticket);
42     XMLReader reader;
43     try {
44       SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
45       factory.setNamespaceAware(true);
46       reader = factory.newSAXParser().getXMLReader();
47     } catch (Exception JavaDoc e) {
48       throw new SAXException(e.getMessage());
49     }
50     reader.setContentHandler(importer);
51     reader.parse(new InputSource(stream));
52   }
53
54   public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException {
55     NodeImpl parent = (NodeImpl) tree.peek();
56     String JavaDoc name = (atts.getValue("sv:name") == null) ? atts.getValue("name") : atts.getValue("sv:name");
57     if("sv:root".equals(name)){
58       return;
59     }
60     String JavaDoc curPath = parent.getPath() + (parent.getPath().endsWith("/") ? "" : "/") + name;
61     if (qName.equals("sv:node")) {
62       try {
63         Node node = parent.addNode(curPath);
64         tree.push(node);
65       } catch (Exception JavaDoc e) {
66         throw new SAXException(e.getMessage());
67       }
68     } else if (qName.equals("sv:property")) {
69       //TODO manage UUID
70
try {
71         String JavaDoc st = atts.getValue("sv:type");
72         st = getType(st.substring(3));
73         currentNode = parent;
74         propertyName = name;
75         propertyType = PropertyType.valueFromName(st);
76       } catch (Exception JavaDoc e) {
77         throw new SAXException(e.getMessage(), e);
78       }
79     } else
80       throw new SAXException("Only sv:node and sv:property are allowed!");
81   }
82
83   private String JavaDoc getType(String JavaDoc name) {
84     if (name.equalsIgnoreCase(PropertyType.TYPENAME_STRING)) {
85       return PropertyType.TYPENAME_STRING;
86     } else if (name.equalsIgnoreCase(PropertyType.TYPENAME_BINARY)) {
87       return PropertyType.TYPENAME_BINARY;
88     } else if (name.equalsIgnoreCase(PropertyType.TYPENAME_BOOLEAN)) {
89       return PropertyType.TYPENAME_BOOLEAN;
90     } else if (name.equalsIgnoreCase(PropertyType.TYPENAME_LONG)) {
91       return PropertyType.TYPENAME_LONG;
92     } else if (name.equalsIgnoreCase(PropertyType.TYPENAME_DOUBLE)) {
93       return PropertyType.TYPENAME_DOUBLE;
94     } else if (name.equalsIgnoreCase(PropertyType.TYPENAME_DATE)) {
95       return PropertyType.TYPENAME_DATE;
96     } else if (name.equalsIgnoreCase(PropertyType.TYPENAME_SOFTLINK)) {
97       return PropertyType.TYPENAME_SOFTLINK;
98     } else if (name.equalsIgnoreCase(PropertyType.TYPENAME_REFERENCE)) {
99       return PropertyType.TYPENAME_REFERENCE;
100     } else {
101       throw new IllegalArgumentException JavaDoc("unknown type: " + name);
102     }
103   }
104
105   public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException {
106     if (qName.equals("sv:node"))
107       tree.pop();
108     else if (qName.equals("sv:property")) {
109       propertyName = null;
110       currentNode = null;
111       propertyType = -1;
112     }
113   }
114
115   public void characters(char[] ch, int start, int length) throws SAXException {
116     if (propertyName != null) {
117       StringBuffer JavaDoc str = new StringBuffer JavaDoc();
118       str.append(ch, start, length);
119       try {
120         if(propertyType == PropertyType.BINARY){
121           currentNode.setProperty(propertyName,
122               new ByteArrayInputStream JavaDoc(Base64.decodeBase64(str.toString().getBytes())));
123         } else {
124           currentNode.setProperty(propertyName, new StringValue(new String JavaDoc(str)), propertyType);
125         }
126       } catch (Exception JavaDoc e) {
127         throw new RuntimeException JavaDoc(e.getMessage(), e);
128       }
129     }
130   }
131
132   public void setDocumentLocator(Locator locator) {
133   }
134
135   public void startDocument() throws SAXException {
136   }
137
138   public void endDocument() throws SAXException {
139   }
140
141   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException {
142   }
143
144   public void endPrefixMapping(String JavaDoc prefix) throws SAXException {
145   }
146
147   public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
148   }
149
150   public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException {
151   }
152
153   public void skippedEntity(String JavaDoc name) throws SAXException {
154   }
155 }
156
Popular Tags