1 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 ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Stack ; 18 19 25 26 public class DocNodeImporter implements ContentHandler { 28 29 Stack tree; 30 String [] uris; 31 32 private DocNodeImporter(NodeImpl parent, String [] URIs) { 33 tree = new Stack (); 34 tree.push(parent); 35 uris = URIs; 36 } 37 38 private boolean isAllowedUri(String 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 stream, String [] nsURIs) 46 throws IOException , SAXException { 47 DocNodeImporter importer = new DocNodeImporter(parent, nsURIs); 48 XMLReader reader; 49 try { 50 SAXParserFactory factory = SAXParserFactory.newInstance(); 51 factory.setNamespaceAware(true); 52 reader = factory.newSAXParser().getXMLReader(); 53 54 } catch (Exception 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 namespaceURI, String localName, String qName, Attributes atts) 62 throws SAXException { 63 NodeImpl parent = (NodeImpl) tree.peek(); 66 if("root".equals(qName)) 67 return; 68 NodeImpl node; 69 HashMap props = new HashMap (); 70 String nodeType = "nt:default"; 71 72 if (atts != null) { 73 int length = atts.getLength(); 74 for (int i = 0; i < length; i++) { 75 String propName; 76 String 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 keys = props.keySet().iterator(); 92 while (keys.hasNext()) { 93 String key = (String ) keys.next(); 94 String value = (String ) props.get(key); 95 node.setProperty(key, new StringValue(value)); 96 } 97 } catch (Exception e) { 98 throw new SAXException(e.getMessage(), e); 99 } 100 tree.push(node); 101 } 102 103 public void endElement(String namespaceURI, String localName, String 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 prefix, String uri) throws SAXException { 120 } 121 122 public void endPrefixMapping(String prefix) throws SAXException { 123 } 124 125 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 126 } 127 128 public void processingInstruction(String target, String data) throws SAXException { 129 } 130 131 public void skippedEntity(String name) throws SAXException { 132 } 133 } 134 | Popular Tags |