KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > storage > filesystem > nodedata > DocViewNodeContainer


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.storage.filesystem.nodedata;
7
8 import java.io.ByteArrayInputStream JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Properties JavaDoc;
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 JavaDoc;
44 import org.w3c.dom.NamedNodeMap JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47
48 /**
49  * Created by The eXo Platform SARL .
50  *
51  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
52  * @version $Id: DocViewNodeContainer.java,v 1.13 2004/09/03 09:59:40 geaz Exp $
53  */

54
55 public class DocViewNodeContainer extends BaseNodeContainer {
56   private String JavaDoc resourceId;
57   private XMLQuery query;
58   private SimpleStatementHelper statHelper;
59   private XMLDataManager xmlDataManager;
60   private NamespaceRegistryImpl namespaceRegistry;
61
62   public DocViewNodeContainer(String JavaDoc jcrPath, String JavaDoc 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 JavaDoc(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 JavaDoc e) {
75       throw new RepositoryException("DocViewNodeContainer() failed. Reason: " + e.getMessage());
76     }
77   }
78
79   public NodeData getNodeByPath(String JavaDoc relPath) {
80     if (relPath.length() == 0)
81       return getRootNode();
82     try {
83       XMLWellFormedData result = getNodeData(jcrToXPathNode(relPath));
84       org.w3c.dom.Element JavaDoc dom = ((org.w3c.dom.Document JavaDoc) result.getAsDOM()).getDocumentElement();
85       NamedNodeMap JavaDoc attrs = dom.getAttributes();
86       ArrayList JavaDoc props = new ArrayList JavaDoc();
87       for (int i = 0; i < attrs.getLength(); i++) {
88         String JavaDoc name = ((Attr JavaDoc) attrs.item(i)).getName();
89         String JavaDoc val = ((Attr JavaDoc) 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 JavaDoc e) {
97       throw new RuntimeException JavaDoc("getNodeBypath failed. Reason:" + e.getMessage());
98     }
99   }
100
101   public List JavaDoc getChildren(String JavaDoc relPath) {
102     ArrayList JavaDoc list = new ArrayList JavaDoc();
103     try {
104       XMLFragmentData result = getChildrenData(jcrToXPathChildren(relPath));
105
106       NodeList JavaDoc nodes = result.getAsNodeList();
107       for (int i = 0; i < nodes.getLength(); i++) {
108         String JavaDoc name = nodes.item(i).getNodeName();
109 // NodeImpl node = new NodeImpl(getJcrPath(relPath) + "/" + name);
110
// list.add(node);
111
list.add(getJcrPath(relPath) + "/" + name);
112       }
113     } catch (PathNotFoundException e) {
114       return list;
115     } catch (Exception JavaDoc e) {
116       throw new RuntimeException JavaDoc("getChildren failed. Reason:" + e.getMessage());
117     }
118     return list;
119   }
120
121   public void add(Node node) throws ItemExistsException, RepositoryException {
122     try {
123       String JavaDoc 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 JavaDoc fos = new FileOutputStream JavaDoc(storage);
134           fos.write(writer.getBytes());
135           fos.close();
136           return;
137         }
138       }
139       insertContent((NodeImpl) node, writer);
140       ByteArrayInputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(writer.getBytes());
141       XMLWellFormedData data = xmlDataManager.create(new InputSource JavaDoc(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 JavaDoc e) {
147       throw new RepositoryException("add node failed. Reason:" + e.getMessage());
148     }
149   }
150
151   public void update(Node node) {
152     try {
153       String JavaDoc 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 JavaDoc stream = new ByteArrayInputStream JavaDoc(writer.getBytes());
163       XMLWellFormedData data = xmlDataManager.create(new InputSource JavaDoc(stream));
164       Statement stat = statHelper.update(parseRelPath(node.getPath()), resourceId, data);
165       query.prepare(stat);
166       query.execute();
167       query.serialize();
168     } catch (Exception JavaDoc e) {
169       throw new RuntimeException JavaDoc("update node failed. Reason:" + e.getMessage());
170     }
171
172   }
173
174   public void delete(String JavaDoc absPath) {
175     try {
176       String JavaDoc 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 JavaDoc e) {
185       e.printStackTrace();
186       throw new RuntimeException JavaDoc();
187     }
188   }
189
190   ///////////////////////////////////////
191
private String JavaDoc jcrToXPathNode(String JavaDoc path) {
192     return path;
193   }
194
195   private String JavaDoc jcrToXPathChildren(String JavaDoc path) {
196     // return path + "/@*|*";
197
return path + "/*";
198   }
199
200   private String JavaDoc getAppendSuffix() {
201     return "/text()[last()]";
202   }
203
204   private XMLWellFormedData getNodeData(String JavaDoc 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 JavaDoc("getNodeData failed. Reason:" + e.getMessage());
218     } catch (InvalidSourceException e) {
219       throw new RuntimeException JavaDoc("getNodeData failed. Reason:" + e.getMessage());
220     } catch (UniFormTransformationException e) {
221       throw new RuntimeException JavaDoc("getNodeData failed. Reason:" + e.getMessage());
222     } catch (QueryRunTimeException e) {
223       throw new RuntimeException JavaDoc("getNodeData failed. Reason:" + e.getMessage());
224     }
225   }
226
227   private XMLFragmentData getChildrenData(String JavaDoc 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 JavaDoc("getChildrenData failed. Reason:" + e.getMessage());
241     } catch (InvalidSourceException e) {
242       throw new RuntimeException JavaDoc("getChildrenData failed. Reason:" + e.getMessage());
243     } catch (UniFormTransformationException e) {
244       throw new RuntimeException JavaDoc("getChildrenData failed. Reason:" + e.getMessage());
245     } catch (QueryRunTimeException e) {
246       throw new RuntimeException JavaDoc("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       // hasChildren = false;
255
Properties JavaDoc attrs = new Properties JavaDoc();
256       PropertyIterator props = node.getProperties();
257       while (props.hasNext()) {
258         PropertyImpl prop = (PropertyImpl) props.next();
259         String JavaDoc 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 /*
270             Iterator nodes = getChildren(parseRelPath(node.getPath())).iterator();
271             System.out.println("Update Child --- " + nodes);
272 // NodeIterator nodes = node.getNodes();
273             while (nodes.hasNext()) {
274                 NodeImpl child = (NodeImpl)nodes.next();
275             System.out.println("Update Child --- " + child);
276
277                 updateContent(child, writer);
278
279             }
280 */

281       writer.writeText(" ");
282       writer.endElement();
283       System.out.println("Update Child --- " + writer);
284     } catch (Exception JavaDoc e) {
285       e.printStackTrace();
286       throw new RuntimeException JavaDoc();
287     }
288   }
289
290   private void insertContent(NodeImpl node, XMLWriter writer) {
291     try {
292       boolean hasChildren = false;
293       Properties JavaDoc attrs = new Properties JavaDoc();
294       PropertyIterator props = node.getProperties();
295       while (props.hasNext()) {
296         PropertyImpl prop = (PropertyImpl) props.next();
297         String JavaDoc strPropVal = DocNodeExporter.getStrPropValue(prop, false);
298         attrs.setProperty(prop.getName(), strPropVal);
299       }
300       writer.startElement(node.getName(), attrs);
301 // Iterator nodes = node.getChangedNodes().iterator();
302
NodeIterator nodes = node.getNodes();
303       while (nodes.hasNext()) {
304         NodeImpl child = (NodeImpl) nodes.next();
305         insertContent(child, writer);
306       }
307 // if (hasChildren == false) {
308
String JavaDoc relPath = jcrToXPathChildren(parseRelPath(node.getPath())); //getParentRelPath(parseRelPath(node.getPath()));
309
if (getParentRelPath(parseRelPath(node.getPath())).length() > 0) {
310         try {
311 // getNodeData(relPath + "/node()");
312
getChildrenData(relPath);
313           hasChildren = true;
314         } catch (PathNotFoundException e) {
315           // still false if not found
316
System.out.println("getNodeData not found --- " + relPath);
317         }
318 // }
319
}
320 // System.out.println("HAS Children --- " + hasChildren);
321
if (!hasChildren)
322         writer.writeText("eXo");
323       writer.endElement();
324     } catch (Exception JavaDoc e) {
325       e.printStackTrace();
326       throw new RuntimeException JavaDoc();
327     }
328   }
329
330
331   private void cleanNode(String JavaDoc 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 JavaDoc e) {
339       e.printStackTrace();
340       throw new RuntimeException JavaDoc();
341     }
342   }
343
344   private void cleanContent() {
345     try {
346       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(storage);
347       fos.write(new byte[0]);
348       fos.close();
349     } catch (Exception JavaDoc e) {
350       e.printStackTrace();
351       throw new RuntimeException JavaDoc();
352     }
353   }
354
355 }
356
Popular Tags