KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > mdr > MDRDataObject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.mdr;
20
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.io.File JavaDoc;
25 import org.openide.ErrorManager;
26 import org.openide.filesystems.FileObject;
27 import org.openide.loaders.DataNode;
28 import org.openide.loaders.DataObjectExistsException;
29 import org.openide.loaders.MultiDataObject;
30 import org.openide.loaders.MultiFileLoader;
31 import org.openide.nodes.Node;
32
33 /**
34  *
35  * @author mmatula
36  * @version
37  */

38 public class MDRDataObject extends MultiDataObject {
39     private final String JavaDoc name;
40     private final MDRDescriptor descriptor;
41     
42     public MDRDataObject(FileObject primaryFile, MultiFileLoader fl) throws DataObjectExistsException {
43         super(primaryFile, fl);
44
45         String JavaDoc fileName = primaryFile.getName();
46         int pos = fileName.indexOf('[');
47         int pos2 = fileName.indexOf(']');
48         
49         this.name = fileName.substring(0, pos);
50         String JavaDoc className = fileName.substring(pos + 1, pos2).replace('-', '.');
51         Map JavaDoc attributes = new HashMap JavaDoc();
52
53         String JavaDoc attrName;
54         
55         for (Enumeration JavaDoc en = primaryFile.getAttributes(); en.hasMoreElements();) {
56             attrName = (String JavaDoc) en.nextElement();
57             attributes.put(attrName, resolveTags(primaryFile.getAttribute(attrName)));
58         }
59         
60         this.descriptor = new MDRDescriptor(className, attributes);
61     }
62     
63     public MDRDescriptor getDescriptor() {
64         return descriptor;
65     }
66     
67     public String JavaDoc getName() {
68         return name;
69     }
70     
71     public boolean isCopyAllowed() {
72         return false;
73     }
74     
75     public boolean isDeleteAllowed() {
76         return false;
77     }
78     
79     public boolean isMoveAllowed() {
80         return false;
81     }
82     
83     public boolean isRenameAllowed() {
84         return false;
85     }
86     
87     public boolean isShadowAllowed() {
88         return false;
89     }
90     
91     public Node createNodeDelegate() {
92         DataNode dataNode = (DataNode) super.createNodeDelegate();
93         dataNode.setIconBase("/org/netbeans/modules/mdr/resources/repository");
94         return dataNode;
95     }
96     
97     private static final String JavaDoc TAG_START = "{";
98     private static final String JavaDoc TAG_END = "}";
99     private static final String JavaDoc TAG_BODY_FOLDER = "folder.";
100     
101     private Object JavaDoc resolveTags(Object JavaDoc attrValue) {
102         if (attrValue instanceof String JavaDoc) {
103             String JavaDoc value = (String JavaDoc) attrValue;
104             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "tag: " + value);
105             StringBuffer JavaDoc result = new StringBuffer JavaDoc(value.length());
106             int pos;
107             int ppos = 0;
108             
109             while ((pos = value.indexOf(TAG_START, ppos)) > -1) {
110                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "tag found at pos: " + pos);
111                 result.append(value.substring(ppos, pos));
112                 ErrorManager.getDefault().log("temp. result: " + result);
113                 ppos = value.indexOf(TAG_END, pos);
114                 ErrorManager.getDefault().log("tag end found at: " + ppos);
115                 if (ppos == -1) {
116                     ppos = pos;
117                     break;
118                 }
119                 result.append(getTagValue(value.substring(pos + 1, ppos)));
120                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "added tag value, temp. result: " + result);
121                 ppos++;
122             }
123             
124             result.append(value.substring(ppos));
125             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "finished, result: " + result);
126             return result.toString();
127         } else {
128             return attrValue;
129         }
130     }
131     
132     private String JavaDoc getTagValue(String JavaDoc tagName) {
133         ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "replacing tag: " + tagName);
134         if (tagName.startsWith(TAG_BODY_FOLDER)) {
135             String JavaDoc folder = tagName.substring(TAG_BODY_FOLDER.length()).replace('\\', '/');
136             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "found folder: " + folder);
137             String JavaDoc resultFolder = System.getProperty("netbeans.user") + "/var/cache/" + folder;
138             try {
139                 File JavaDoc result = new File JavaDoc(resultFolder);
140                 result.mkdirs();
141                 return resultFolder;
142             } catch (Exception JavaDoc e) {
143                 ErrorManager.getDefault().notify(e);
144                 return "";
145             }
146         } else {
147             return TAG_START + tagName + TAG_END;
148         }
149     }
150 }
151
Popular Tags