1 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 ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.ByteArrayInputStream ; 17 import java.util.Stack ; 18 19 25 26 public class SysNodeImporter implements ContentHandler { 27 28 private Stack tree; 29 private String propertyName; 30 private int propertyType; 31 private NodeImpl currentNode; 32 33 34 private SysNodeImporter(String rootPath, Ticket ticket) throws RepositoryException { 35 tree = new Stack (); 36 tree.push(ticket.getNodeByAbsPath(rootPath)); 37 } 38 39 public static void fillItems(String rootPath, InputStream stream, Ticket ticket) 40 throws IOException , SAXException, RepositoryException { 41 SysNodeImporter importer = new SysNodeImporter(rootPath, ticket); 42 XMLReader reader; 43 try { 44 SAXParserFactory factory = SAXParserFactory.newInstance(); 45 factory.setNamespaceAware(true); 46 reader = factory.newSAXParser().getXMLReader(); 47 } catch (Exception 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 namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 55 NodeImpl parent = (NodeImpl) tree.peek(); 56 String name = (atts.getValue("sv:name") == null) ? atts.getValue("name") : atts.getValue("sv:name"); 57 if("sv:root".equals(name)){ 58 return; 59 } 60 String 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 e) { 66 throw new SAXException(e.getMessage()); 67 } 68 } else if (qName.equals("sv:property")) { 69 try { 71 String 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 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 getType(String 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 ("unknown type: " + name); 102 } 103 } 104 105 public void endElement(String namespaceURI, String localName, String 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 str = new StringBuffer (); 118 str.append(ch, start, length); 119 try { 120 if(propertyType == PropertyType.BINARY){ 121 currentNode.setProperty(propertyName, 122 new ByteArrayInputStream (Base64.decodeBase64(str.toString().getBytes()))); 123 } else { 124 currentNode.setProperty(propertyName, new StringValue(new String (str)), propertyType); 125 } 126 } catch (Exception e) { 127 throw new RuntimeException (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 prefix, String uri) throws SAXException { 142 } 143 144 public void endPrefixMapping(String prefix) throws SAXException { 145 } 146 147 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 148 } 149 150 public void processingInstruction(String target, String data) throws SAXException { 151 } 152 153 public void skippedEntity(String name) throws SAXException { 154 } 155 } 156 | Popular Tags |