1 5 6 package org.exoplatform.services.jcr.impl.storage.filesystem.nodedata; 7 8 import java.io.ByteArrayInputStream ; 9 import java.io.File ; 10 import java.io.FileOutputStream ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 import java.util.Properties ; 14 15 import javax.jcr.ItemExistsException; 16 import javax.jcr.NamespaceRegistry; 17 import javax.jcr.Node; 18 import javax.jcr.NodeIterator; 19 import javax.jcr.PathNotFoundException; 20 import javax.jcr.PropertyIterator; 21 import javax.jcr.PropertyType; 22 import javax.jcr.RepositoryException; 23 import javax.jcr.StringValue; 24 25 import org.exoplatform.container.PortalContainer; 26 import org.exoplatform.services.jcr.core.NodeData; 27 import org.exoplatform.services.jcr.impl.core.NamespaceRegistryImpl; 28 import org.exoplatform.services.jcr.impl.core.NodeImpl; 29 import org.exoplatform.services.jcr.impl.core.PropertyImpl; 30 import org.exoplatform.services.jcr.impl.util.DocNodeExporter; 31 import org.exoplatform.services.jcr.impl.util.XMLWriter; 32 import org.exoplatform.services.xml.querying.InvalidSourceException; 33 import org.exoplatform.services.xml.querying.InvalidStatementException; 34 import org.exoplatform.services.xml.querying.QueryRunTimeException; 35 import org.exoplatform.services.xml.querying.Statement; 36 import org.exoplatform.services.xml.querying.UniFormTransformationException; 37 import org.exoplatform.services.xml.querying.XMLFragmentData; 38 import org.exoplatform.services.xml.querying.XMLQuery; 39 import org.exoplatform.services.xml.querying.XMLQueryingService; 40 import org.exoplatform.services.xml.querying.XMLWellFormedData; 41 import org.exoplatform.services.xml.querying.helper.SimpleStatementHelper; 42 import org.exoplatform.services.xml.querying.helper.XMLDataManager; 43 import org.w3c.dom.Attr ; 44 import org.w3c.dom.NamedNodeMap ; 45 import org.w3c.dom.NodeList ; 46 import org.xml.sax.InputSource ; 47 48 54 55 public class DocViewNodeContainer extends BaseNodeContainer { 56 private String resourceId; 57 private XMLQuery query; 58 private SimpleStatementHelper statHelper; 59 private XMLDataManager xmlDataManager; 60 private NamespaceRegistryImpl namespaceRegistry; 61 62 public DocViewNodeContainer(String jcrPath, String resourceId) throws RepositoryException { 63 this.namespaceRegistry = (NamespaceRegistryImpl) PortalContainer.getInstance(). 64 getComponentInstancesOfType(NamespaceRegistry.class); 65 this.containerPath = jcrPath; 66 this.nodeType = "exo:jcrdocfile"; 67 this.resourceId = resourceId; 68 this.storage = new File (resourceId); 69 try { 70 XMLQueryingService qService = (XMLQueryingService) PortalContainer.getInstance().getComponentInstanceOfType(XMLQueryingService.class); 71 query = qService.createQuery(); 72 statHelper = qService.createStatementHelper(); 73 xmlDataManager = qService.createXMLDataManager(); 74 } catch (Exception e) { 75 throw new RepositoryException("DocViewNodeContainer() failed. Reason: " + e.getMessage()); 76 } 77 } 78 79 public NodeData getNodeByPath(String relPath) { 80 if (relPath.length() == 0) 81 return getRootNode(); 82 try { 83 XMLWellFormedData result = getNodeData(jcrToXPathNode(relPath)); 84 org.w3c.dom.Element dom = ((org.w3c.dom.Document ) result.getAsDOM()).getDocumentElement(); 85 NamedNodeMap attrs = dom.getAttributes(); 86 ArrayList props = new ArrayList (); 87 for (int i = 0; i < attrs.getLength(); i++) { 88 String name = ((Attr ) attrs.item(i)).getName(); 89 String val = ((Attr ) attrs.item(i)).getValue(); 90 PropertyImpl p = new PropertyImpl(getJcrPath(relPath) + "/" + name, new StringValue(val), PropertyType.STRING); 91 props.add(p); 92 } 93 return new NodeImpl(getJcrPath(relPath), props); 94 } catch (PathNotFoundException e) { 95 return null; 96 } catch (Exception e) { 97 throw new RuntimeException ("getNodeBypath failed. Reason:" + e.getMessage()); 98 } 99 } 100 101 public List getChildren(String relPath) { 102 ArrayList list = new ArrayList (); 103 try { 104 XMLFragmentData result = getChildrenData(jcrToXPathChildren(relPath)); 105 106 NodeList nodes = result.getAsNodeList(); 107 for (int i = 0; i < nodes.getLength(); i++) { 108 String name = nodes.item(i).getNodeName(); 109 list.add(getJcrPath(relPath) + "/" + name); 112 } 113 } catch (PathNotFoundException e) { 114 return list; 115 } catch (Exception e) { 116 throw new RuntimeException ("getChildren failed. Reason:" + e.getMessage()); 117 } 118 return list; 119 } 120 121 public void add(Node node) throws ItemExistsException, RepositoryException { 122 try { 123 String relPath = parseRelPath(node.getPath()); 124 System.out.println("Rel Path --- " + relPath); 125 XMLWriter writer = new XMLWriter(namespaceRegistry.getURIMap()); 126 if (relPath.length() == 0) 127 return; 128 else { 129 if (relPath.equals("/jcr:content")) { 130 if (storage.length() > 0) 131 throw new ItemExistsException("File <" + resourceId + "> is not empty!"); 132 insertContent((NodeImpl) node, writer); 133 FileOutputStream fos = new FileOutputStream (storage); 134 fos.write(writer.getBytes()); 135 fos.close(); 136 return; 137 } 138 } 139 insertContent((NodeImpl) node, writer); 140 ByteArrayInputStream stream = new ByteArrayInputStream (writer.getBytes()); 141 XMLWellFormedData data = xmlDataManager.create(new InputSource (stream)); 142 Statement stat = statHelper.append(getParentRelPath(parseRelPath(node.getPath())) + getAppendSuffix(), resourceId, data); 143 query.prepare(stat); 144 query.execute(); 145 query.serialize(); 146 } catch (Exception e) { 147 throw new RepositoryException("add node failed. Reason:" + e.getMessage()); 148 } 149 } 150 151 public void update(Node node) { 152 try { 153 String relPath = parseRelPath(node.getPath()); 154 System.out.println("Rel Path --- " + relPath); 155 XMLWriter writer; 156 if (relPath.length() == 0) 157 return; 158 159 writer = new XMLWriter(); 160 161 updateContent((NodeImpl) node, writer); 162 ByteArrayInputStream stream = new ByteArrayInputStream (writer.getBytes()); 163 XMLWellFormedData data = xmlDataManager.create(new InputSource (stream)); 164 Statement stat = statHelper.update(parseRelPath(node.getPath()), resourceId, data); 165 query.prepare(stat); 166 query.execute(); 167 query.serialize(); 168 } catch (Exception e) { 169 throw new RuntimeException ("update node failed. Reason:" + e.getMessage()); 170 } 171 172 } 173 174 public void delete(String absPath) { 175 try { 176 String relPath = parseRelPath(absPath); 177 System.out.println("Delete RelPath <" + relPath + ">"); 178 if (relPath.length() == 0) 179 storage.delete(); 180 else if (relPath.equals("/jcr:content")) 181 cleanContent(); 182 else 183 cleanNode(relPath); 184 } catch (Exception e) { 185 e.printStackTrace(); 186 throw new RuntimeException (); 187 } 188 } 189 190 private String jcrToXPathNode(String path) { 192 return path; 193 } 194 195 private String jcrToXPathChildren(String path) { 196 return path + "/*"; 198 } 199 200 private String getAppendSuffix() { 201 return "/text()[last()]"; 202 } 203 204 private XMLWellFormedData getNodeData(String xPath) throws PathNotFoundException { 205 try { 206 Statement stat = statHelper.select(xPath, resourceId); 207 System.out.println("getNodeData Stat --- " + stat); 208 query.prepare(stat); 209 query.execute(); 210 System.out.println("getNodeData Res --- " + query.getResult()); 211 if (query.getResult().isEmpty()) 212 throw new PathNotFoundException("getNodeData failed. Path<" + xPath + "> not found in " + resourceId); 213 else { 214 return xmlDataManager.toWellFormed(query.getResult()); 215 } 216 } catch (InvalidStatementException e) { 217 throw new RuntimeException ("getNodeData failed. Reason:" + e.getMessage()); 218 } catch (InvalidSourceException e) { 219 throw new RuntimeException ("getNodeData failed. Reason:" + e.getMessage()); 220 } catch (UniFormTransformationException e) { 221 throw new RuntimeException ("getNodeData failed. Reason:" + e.getMessage()); 222 } catch (QueryRunTimeException e) { 223 throw new RuntimeException ("getNodeData failed. Reason:" + e.getMessage()); 224 } 225 } 226 227 private XMLFragmentData getChildrenData(String xPath) throws PathNotFoundException { 228 try { 229 Statement stat = statHelper.select(xPath, resourceId); 230 System.out.println("getChildrenData Stat --- " + stat); 231 query.prepare(stat); 232 query.execute(); 233 System.out.println("getChildrenData Res --- " + query.getResult()); 234 if (query.getResult().isEmpty()) 235 throw new PathNotFoundException("getChildrenData failed. Path<" + xPath + "> not found in " + resourceId); 236 else { 237 return xmlDataManager.toFragment(query.getResult()); 238 } 239 } catch (InvalidStatementException e) { 240 throw new RuntimeException ("getChildrenData failed. Reason:" + e.getMessage()); 241 } catch (InvalidSourceException e) { 242 throw new RuntimeException ("getChildrenData failed. Reason:" + e.getMessage()); 243 } catch (UniFormTransformationException e) { 244 throw new RuntimeException ("getChildrenData failed. Reason:" + e.getMessage()); 245 } catch (QueryRunTimeException e) { 246 throw new RuntimeException ("getChildrenData failed. Reason:" + e.getMessage()); 247 } 248 } 249 250 private void updateContent(NodeImpl node, XMLWriter writer) { 251 try { 252 System.out.println("Update Child --- " + node); 253 254 Properties attrs = new Properties (); 256 PropertyIterator props = node.getProperties(); 257 while (props.hasNext()) { 258 PropertyImpl prop = (PropertyImpl) props.next(); 259 String strPropVal = DocNodeExporter.getStrPropValue(prop, false); 260 attrs.setProperty(prop.getName(), strPropVal); 261 } 262 writer.startElement(node.getName(), attrs); 263 264 try { 265 writer.writeText(getChildrenData(jcrToXPathChildren(parseRelPath(node.getPath()))).toString()); 266 } catch (PathNotFoundException e) { 267 } 268 269 281 writer.writeText(" "); 282 writer.endElement(); 283 System.out.println("Update Child --- " + writer); 284 } catch (Exception e) { 285 e.printStackTrace(); 286 throw new RuntimeException (); 287 } 288 } 289 290 private void insertContent(NodeImpl node, XMLWriter writer) { 291 try { 292 boolean hasChildren = false; 293 Properties attrs = new Properties (); 294 PropertyIterator props = node.getProperties(); 295 while (props.hasNext()) { 296 PropertyImpl prop = (PropertyImpl) props.next(); 297 String strPropVal = DocNodeExporter.getStrPropValue(prop, false); 298 attrs.setProperty(prop.getName(), strPropVal); 299 } 300 writer.startElement(node.getName(), attrs); 301 NodeIterator nodes = node.getNodes(); 303 while (nodes.hasNext()) { 304 NodeImpl child = (NodeImpl) nodes.next(); 305 insertContent(child, writer); 306 } 307 String relPath = jcrToXPathChildren(parseRelPath(node.getPath())); if (getParentRelPath(parseRelPath(node.getPath())).length() > 0) { 310 try { 311 getChildrenData(relPath); 313 hasChildren = true; 314 } catch (PathNotFoundException e) { 315 System.out.println("getNodeData not found --- " + relPath); 317 } 318 } 320 if (!hasChildren) 322 writer.writeText("eXo"); 323 writer.endElement(); 324 } catch (Exception e) { 325 e.printStackTrace(); 326 throw new RuntimeException (); 327 } 328 } 329 330 331 private void cleanNode(String relPath) { 332 try { 333 Statement stat = statHelper.delete(jcrToXPathNode(relPath), resourceId); 334 System.out.println("clean Content Stat --- " + stat); 335 query.prepare(stat); 336 query.execute(); 337 query.serialize(); 338 } catch (Exception e) { 339 e.printStackTrace(); 340 throw new RuntimeException (); 341 } 342 } 343 344 private void cleanContent() { 345 try { 346 FileOutputStream fos = new FileOutputStream (storage); 347 fos.write(new byte[0]); 348 fos.close(); 349 } catch (Exception e) { 350 e.printStackTrace(); 351 throw new RuntimeException (); 352 } 353 } 354 355 } 356 | Popular Tags |