KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > packaging > projects > Project


1 /*
2  * This software is OSI Certified Open Source Software.
3  * OSI Certified is a certification mark of the Open Source Initiative.
4  * The license (Mozilla version 1.0) can be read at the MMBase site.
5  * See http://www.MMBase.org/license
6  */

7 package org.mmbase.applications.packaging.projects;
8
9 import java.io.DataOutputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import org.mmbase.applications.packaging.ProjectManager;
16 import org.mmbase.applications.packaging.projects.creators.CreatorInterface;
17 import org.mmbase.applications.packaging.util.ExtendedDocumentReader;
18 import org.mmbase.util.XMLEntityResolver;
19 import org.mmbase.util.logging.Logger;
20 import org.mmbase.util.logging.Logging;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.NamedNodeMap JavaDoc;
23
24 /**
25  * @author Daniel Ockeloen
26  *
27  */

28 public class Project {
29
30     // logger
31
private static Logger log = Logging.getLoggerInstance(Project.class);
32
33     String JavaDoc path;
34     String JavaDoc name;
35     String JavaDoc basedir;
36     HashMap JavaDoc targets = new HashMap JavaDoc();
37     HashMap JavaDoc packagetargets = new HashMap JavaDoc();
38     HashMap JavaDoc bundletargets = new HashMap JavaDoc();
39
40     /**
41      * DTD resource filename of the packaging DTD version 1.0
42      */

43     public final static String JavaDoc DTD_PACKAGING_1_0 = "packaging_1_0.dtd";
44
45     /**
46      * Public ID of the packaging DTD version 1.0
47      */

48     public final static String JavaDoc PUBLIC_ID_PACKAGING_1_0 = "-//MMBase//DTD packaging config 1.0//EN";
49
50
51     /**
52      * Register the Public Ids for DTDs used by DatabaseReader
53      * This method is called by XMLEntityResolver.
54      */

55     public static void registerPublicIDs() {
56         XMLEntityResolver.registerPublicID(PUBLIC_ID_PACKAGING_1_0, "DTD_PACKAGING_1_0", Project.class);
57     }
58
59
60     /**
61      *Constructor for the Project object
62      *
63      * @param name Description of the Parameter
64      * @param path Description of the Parameter
65      */

66     public Project(String JavaDoc name, String JavaDoc path) {
67         this.name = name;
68         this.path = path;
69         basedir = expandBasedir(".");
70         // set basedir to default
71
readTargets();
72     }
73
74
75     /**
76      * Sets the name attribute of the Project object
77      *
78      * @param name The new name value
79      */

80     public void setName(String JavaDoc name) {
81         this.name = name;
82     }
83
84
85     /**
86      * Sets the path attribute of the Project object
87      *
88      * @param path The new path value
89      */

90     public void setPath(String JavaDoc path) {
91         this.path = path;
92     }
93
94
95     /**
96      * Gets the path attribute of the Project object
97      *
98      * @return The path value
99      */

100     public String JavaDoc getPath() {
101         return path;
102     }
103
104     public String JavaDoc getDir() {
105     if (path!=null) {
106         int pos=path.lastIndexOf("/");
107         if (pos==-1) pos=path.lastIndexOf("\\");
108         if (pos!=-1) return path.substring(0,pos+1);
109     }
110         return null;
111     }
112
113     /**
114      * Gets the name attribute of the Project object
115      *
116      * @return The name value
117      */

118     public String JavaDoc getName() {
119         return name;
120     }
121
122
123     /**
124      * Gets the baseDir attribute of the Project object
125      *
126      * @return The baseDir value
127      */

128     public String JavaDoc getBaseDir() {
129         return basedir;
130     }
131
132
133     /**
134      * Gets the targets attribute of the Project object
135      *
136      * @return The targets value
137      */

138     public Iterator JavaDoc getTargets() {
139         return targets.values().iterator();
140     }
141
142
143     /**
144      * Description of the Method
145      *
146      * @param name Description of the Parameter
147      * @return Description of the Return Value
148      */

149     public boolean deleteTarget(String JavaDoc name) {
150         // bad bad bad
151
targets.remove(name);
152         packagetargets.remove(name);
153         bundletargets.remove(name);
154         save();
155         return true;
156     }
157
158
159     /**
160      * Adds a feature to the BundleTarget attribute of the Project object
161      *
162      * @param name The feature to be added to the BundleTarget attribute
163      * @param type The feature to be added to the BundleTarget attribute
164      * @param path The feature to be added to the BundleTarget attribute
165      * @return Description of the Return Value
166      */

167     public boolean addBundleTarget(String JavaDoc name, String JavaDoc type, String JavaDoc path) {
168         // several name tricks to make allow for faster creation by people
169
if (name.equals("") || name.equals("[auto]")) {
170             name = type.substring(type.indexOf("/") + 1);
171         }
172         if (path.equals("") || path.equals("[auto]")) {
173             path = "packaging" + File.separator + getName() + "_" + type.replace('/','_') + ".xml";
174             path = path.replace(' ', '_');
175         }
176
177         // check if the dirs are created, if not create them
178
String JavaDoc dirsp = basedir + path.substring(0, path.lastIndexOf(File.separator));
179         File JavaDoc dirs = new File JavaDoc(dirsp);
180         if (!dirs.exists()) {
181             dirs.mkdirs();
182         }
183
184         Target t = new Target(this,name);
185         t.setBundle(true);
186         t.setBaseDir(basedir);
187         t.setPath(path);
188         t.setType(type);
189         // get the handler
190
CreatorInterface cr = ProjectManager.getCreatorByType(type);
191         if (cr != null) {
192             t.setCreator(cr);
193         }
194         bundletargets.put(name, t);
195         save();
196         return true;
197     }
198
199
200     /**
201      * Adds a feature to the PackageTarget attribute of the Project object
202      *
203      * @param name The feature to be added to the PackageTarget attribute
204      * @param type The feature to be added to the PackageTarget attribute
205      * @param path The feature to be added to the PackageTarget attribute
206      * @return Description of the Return Value
207      */

208     public boolean addPackageTarget(String JavaDoc name, String JavaDoc type, String JavaDoc path) {
209         CreatorInterface cr = ProjectManager.getCreatorByType(type);
210         if (name.equals("") || name.equals("[auto]")) {
211         if (cr != null) {
212         name = cr.getDefaultTargetName();
213         name = checkDubName(name);
214         } else {
215                 name = type.substring(type.indexOf("/") + 1);
216         }
217         }
218         if (path.equals("") || path.equals("[auto]")) {
219             path = "packaging" + File.separator + getName() + "_" + type.replace('/','_') + ".xml";
220             path = path.replace(' ', '_');
221         path = checkDubFilename(path);
222         }
223         // check if the dirs are created, if not create them
224
String JavaDoc dirsp = basedir + path.substring(0, path.lastIndexOf(File.separator));
225         File JavaDoc dirs = new File JavaDoc(dirsp);
226         if (!dirs.exists()) {
227             dirs.mkdirs();
228         }
229         Target t = new Target(this,name);
230         t.setPath(path);
231         t.setBaseDir(basedir);
232         t.setType(type);
233         if (cr != null) {
234             t.setCreator(cr);
235             t.setDefaults();
236         }
237         packagetargets.put(name, t);
238         save();
239         return true;
240     }
241
242
243     /**
244      * Gets the target attribute of the Project object
245      *
246      * @param name Description of the Parameter
247      * @return The target value
248      */

249     public Target getTarget(String JavaDoc name) {
250         Object JavaDoc o = targets.get(name);
251         if (o != null) {
252             return (Target) o;
253         }
254         o = packagetargets.get(name);
255         if (o != null) {
256             return (Target) o;
257         }
258         o = bundletargets.get(name);
259         if (o != null) {
260             return (Target) o;
261         }
262         return null;
263     }
264
265
266     /**
267      * Gets the target attribute of the Project object
268      *
269      * @param name Description of the Parameter
270      * @return The target value
271      */

272     public Target getTargetById(String JavaDoc id) {
273     // find the target with the same package id
274
Iterator JavaDoc e = packagetargets.values().iterator();
275         while (e.hasNext()) {
276             Target t = (Target) e.next();
277         if (t.getId().equals(id)) {
278         return t;
279         }
280     }
281         return null;
282     }
283
284
285     /**
286      * Gets the packageTargets attribute of the Project object
287      *
288      * @return The packageTargets value
289      */

290     public Iterator JavaDoc getPackageTargets() {
291         return packagetargets.values().iterator();
292     }
293
294
295     /**
296      * Gets the bundleTargets attribute of the Project object
297      *
298      * @return The bundleTargets value
299      */

300     public Iterator JavaDoc getBundleTargets() {
301         return bundletargets.values().iterator();
302     }
303
304
305     /**
306      * Description of the Method
307      */

308     public void readTargets() {
309         File JavaDoc file = new File JavaDoc(path);
310         if (file.exists()) {
311             ExtendedDocumentReader reader = new ExtendedDocumentReader(path, Project.class);
312             if (reader != null) {
313
314                 org.w3c.dom.Node JavaDoc n2 = reader.getElementByPath("packaging");
315                 if (n2 != null) {
316                     NamedNodeMap JavaDoc nm = n2.getAttributes();
317                     if (nm != null) {
318                         org.w3c.dom.Node JavaDoc n3 = nm.getNamedItem("basedir");
319                         if (n3 != null) {
320                             basedir = n3.getNodeValue();
321                             if (basedir.equals(".")) {
322                                 basedir = expandBasedir(basedir);
323                             }
324                         }
325                     }
326                 }
327
328                 // decode targets
329
for (Iterator JavaDoc ns = reader.getChildElements("packaging", "target"); ns.hasNext(); ) {
330                     Element JavaDoc n = (Element JavaDoc) ns.next();
331                     NamedNodeMap JavaDoc nm = n.getAttributes();
332                     if (nm != null) {
333                         String JavaDoc name = null;
334                         String JavaDoc depends = null;
335
336                         // decode name
337
org.w3c.dom.Node JavaDoc n3 = nm.getNamedItem("name");
338                         if (n3 != null) {
339                             name = n3.getNodeValue();
340                         }
341                         // decode depends
342
n3 = nm.getNamedItem("depends");
343                         if (n3 != null) {
344                             depends = n3.getNodeValue();
345                         }
346
347                         if (name != null) {
348                             Target t = new Target(this,name);
349                             if (depends != null) {
350                                 t.setDepends(depends);
351                             }
352                             targets.put(name, t);
353                         }
354                     }
355                 }
356
357                 // decode packagetargets
358
for (Iterator JavaDoc ns = reader.getChildElements("packaging", "package"); ns.hasNext(); ) {
359                     Element JavaDoc n = (Element JavaDoc) ns.next();
360                     NamedNodeMap JavaDoc nm = n.getAttributes();
361                     if (nm != null) {
362                         String JavaDoc name = null;
363                         String JavaDoc type = null;
364                         String JavaDoc path = null;
365
366                         // decode name
367
org.w3c.dom.Node JavaDoc n3 = nm.getNamedItem("name");
368                         if (n3 != null) {
369                             name = n3.getNodeValue();
370                         }
371                         // decode path
372
n3 = nm.getNamedItem("file");
373                         if (n3 != null) {
374                             path = n3.getNodeValue();
375                         }
376
377                         // decode type
378
n3 = nm.getNamedItem("type");
379                         if (n3 != null) {
380                             type = n3.getNodeValue();
381                         }
382
383                         if (name != null) {
384                             Target t = new Target(this,name);
385                             if (path != null) {
386                                 t.setBaseDir(basedir);
387                                 t.setPath(path);
388                             }
389                             if (type != null) {
390                                 t.setType(type);
391                                 // get the handler
392
CreatorInterface cr = ProjectManager.getCreatorByType(type);
393                                 if (cr != null) {
394                                     t.setCreator(cr);
395                                 }
396                             }
397                             packagetargets.put(name, t);
398                         }
399                     }
400                 }
401
402                 // decode bundletargets
403
for (Iterator JavaDoc ns = reader.getChildElements("packaging", "bundle"); ns.hasNext(); ) {
404                     Element JavaDoc n = (Element JavaDoc) ns.next();
405                     NamedNodeMap JavaDoc nm = n.getAttributes();
406                     if (nm != null) {
407                         String JavaDoc name = null;
408                         String JavaDoc type = "bundle/basic";
409                         String JavaDoc depends = null;
410                         String JavaDoc path = null;
411
412                         // decode name
413
org.w3c.dom.Node JavaDoc n3 = nm.getNamedItem("name");
414                         if (n3 != null) {
415                             name = n3.getNodeValue();
416                         }
417                         // decode type
418
n3 = nm.getNamedItem("type");
419                         if (n3 != null) {
420                             type = n3.getNodeValue();
421                         }
422                         // decode path
423
n3 = nm.getNamedItem("file");
424                         if (n3 != null) {
425                             path = n3.getNodeValue();
426                         }
427                         // decode depends
428
n3 = nm.getNamedItem("depends");
429                         if (n3 != null) {
430                             depends = n3.getNodeValue();
431                         }
432
433                         if (name != null) {
434                             Target t = new Target(this,name);
435                             t.setBundle(true);
436                             if (depends != null) {
437                                 t.setDepends(depends);
438                             }
439                             if (path != null) {
440                                 t.setPath(path);
441                                 t.setBaseDir(basedir);
442                             }
443                             if (type != null) {
444                                 t.setType(type);
445                                 // get the handler
446
CreatorInterface cr = ProjectManager.getCreatorByType(type);
447                                 if (cr != null) {
448                                     t.setCreator(cr);
449                                 }
450                             }
451
452                             bundletargets.put(name, t);
453                         }
454                     }
455                 }
456
457             }
458         } else {
459             log.error("missing projects file : " + path);
460         }
461     }
462
463
464     /**
465      * Description of the Method
466      *
467      * @param basedir Description of the Parameter
468      * @return Description of the Return Value
469      */

470     private String JavaDoc expandBasedir(String JavaDoc basedir) {
471         File JavaDoc basefile = new File JavaDoc(path);
472         if (basefile != null) {
473             return basefile.getParent() + File.separator;
474         }
475         return basedir;
476     }
477
478
479     /**
480      * Description of the Method
481      *
482      * @return Description of the Return Value
483      */

484     public boolean save() {
485         String JavaDoc body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
486         body += "<!DOCTYPE packaging PUBLIC \"-//MMBase/DTD packaging config 1.0//EN\" \"http://www.mmbase.org/dtd/packaging_1_0.dtd\">\n";
487         body += "<packaging basedir=\".\">\n";
488         Iterator JavaDoc e = packagetargets.values().iterator();
489         while (e.hasNext()) {
490             Target t = (Target) e.next();
491             body += "\t<package name=\"" + t.getName() + "\" type=\"" + t.getType() + "\" file=\"" + t.getPath() + "\" />\n";
492         }
493         e = bundletargets.values().iterator();
494         while (e.hasNext()) {
495             Target t = (Target) e.next();
496             body += "\t<bundle name=\"" + t.getName() + "\" type=\"" + t.getType() + "\" file=\"" + t.getPath() + "\" />\n";
497         }
498         body += "</packaging>\n";
499         File JavaDoc sfile = new File JavaDoc(path);
500
501         // check if the dirs are created, if not create them
502
String JavaDoc dirsp = path.substring(0, path.lastIndexOf(File.separator));
503         File JavaDoc dirs = new File JavaDoc(dirsp);
504         if (!dirs.exists()) {
505             dirs.mkdirs();
506         }
507         try {
508             DataOutputStream JavaDoc scan = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(sfile));
509             scan.writeBytes(body);
510             scan.flush();
511             scan.close();
512         } catch (Exception JavaDoc f) {
513             log.error("Can't save packaging file : " + path);
514             return false;
515         }
516         return true;
517     }
518
519
520     /**
521      * Description of the Method
522      *
523      * @return Description of the Return Value
524      */

525     public boolean hasSyntaxErrors() {
526         // performs several syntax checks to signal
527
// the users in the gui tools on possible problems
528
Iterator JavaDoc e = packagetargets.values().iterator();
529         while (e.hasNext()) {
530             Target t = (Target) e.next();
531             if (t.hasSyntaxErrors()) {
532                 return true;
533             }
534         }
535         e = bundletargets.values().iterator();
536         while (e.hasNext()) {
537             Target t = (Target) e.next();
538             if (t.hasSyntaxErrors()) {
539                 return true;
540             }
541         }
542         return false;
543     }
544
545     private String JavaDoc checkDubName(String JavaDoc name) {
546     boolean dub = true;
547     int counter = 2;
548     String JavaDoc newname = name;
549     while (dub) {
550         Target t = getTarget(newname);
551         if (t != null) {
552             newname = name + (counter++);
553         } else {
554             dub = false;
555         }
556     }
557     return newname;
558     }
559
560
561     private String JavaDoc checkDubFilename(String JavaDoc filename) {
562     boolean dub = true;
563     int counter = 2;
564     String JavaDoc newfilename = filename;
565     while (dub) {
566         File JavaDoc t=new File JavaDoc(basedir+File.separator+newfilename);
567         if (t.exists()) {
568             newfilename = filename.substring(0,filename.length()-4) + (counter++)+".xml";
569         } else {
570             dub = false;
571         }
572     }
573     return newfilename;
574     }
575
576 }
577
578
Popular Tags