KickJava   Java API By Example, From Geeks To Geeks.

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


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
9 import javax.jcr.ItemExistsException;
10 import javax.jcr.PathNotFoundException;
11 import javax.jcr.RepositoryException;
12 import org.exoplatform.services.jcr.core.NodeData;
13 import org.exoplatform.services.jcr.util.PathUtil;
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 /**
18  * Created by The eXo Platform SARL .
19  *
20  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
21  * @version $Id: NodeContainerResolver.java,v 1.13 2004/11/02 18:34:16 geaz Exp $
22  */

23
24 public class NodeContainerResolver {
25
26   // Repository Doc View
27
public static final String JavaDoc DOCVIEW_EXT = ".rdv";
28
29   public NodeContainer resolve(File JavaDoc rootFile, String JavaDoc jcrPath) throws PathNotFoundException,
30       RepositoryException, IOException JavaDoc {
31 // File rootFile = (File) rootContext;
32

33     int depth = PathUtil.getDepth(jcrPath);
34     int i = 0;
35     File JavaDoc container = null;
36 // String filePath = null;
37
String JavaDoc parentType = "nt:folder";
38     String JavaDoc ancestor = null;
39
40
41     for (; i <= depth; i++) {
42       ancestor = PathUtil.getAncestorPath(jcrPath, i);
43       container = new File JavaDoc(rootFile.getCanonicalPath() + ancestor);
44
45       if (container.exists()) {
46         if (container.isDirectory()) {
47           parentType = "nt:folder";
48         } else {
49           if (container.getName().toLowerCase().endsWith(DOCVIEW_EXT))
50             parentType = "exo:jcrdocfile";
51           else
52             parentType = "nt:file";
53         }
54         break;
55       }
56     }
57
58 // System.out.println(">>>.> Resolve container "+container+" "+ancestor+" "+parentType);
59

60     if (parentType == null)
61       throw new PathNotFoundException("File for the <" + jcrPath + "> not found!");
62
63     if (parentType.equals("nt:file"))
64       return new FileNodeContainer(ancestor, container);
65     else if (parentType.equals("nt:folder"))
66       return new FolderNodeContainer(ancestor, container);
67     else if (parentType.equals("exo:jcrdocfile"))
68       return new DocViewNodeContainer(ancestor, container.getCanonicalPath());
69
70     throw new RepositoryException("Unresolved node <" + jcrPath + ">!");
71
72   }
73
74   public NodeContainer create(File JavaDoc rootFile, NodeData node) throws PathNotFoundException,
75       RepositoryException, IOException JavaDoc, ItemExistsException {
76
77     // File should exist
78
String JavaDoc nodeType = node.getPrimaryNodeType().getName();
79     String JavaDoc jcrPath = node.getPath();
80
81     System.out.println("Resolver create ---" + jcrPath + " " + nodeType + " " + rootFile.getCanonicalPath() + jcrPath);
82
83     if (isFile(nodeType)) {
84       File JavaDoc file = new File JavaDoc(rootFile.getCanonicalPath() + jcrPath);
85       if (file.exists())
86         throw new ItemExistsException("File for <" + jcrPath + "> already exists!");
87
88       if ("nt:folder".equals(nodeType)) {
89         file.mkdir();
90       } else {
91         file.createNewFile();
92       }
93     }
94     else if(!"nt:content".equals(nodeType))
95         throw new RepositoryException("FS container can't create node of type <" + nodeType + ">!");
96
97     return resolve(rootFile, jcrPath);
98   }
99
100   private boolean isFile(String JavaDoc type) {
101
102     return type.equals("nt:file") ||
103         type.equals("nt:folder") ||
104         type.equals("exo:jcrdocfile");
105   }
106
107
108 }
109
Popular Tags