KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > LayerIndex


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
20 package org.netbeans.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.SortedMap JavaDoc;
32 import java.util.TreeMap JavaDoc;
33 import java.util.jar.JarFile JavaDoc;
34 import java.util.jar.Manifest JavaDoc;
35 import javax.xml.parsers.SAXParserFactory JavaDoc;
36 import org.apache.tools.ant.BuildException;
37 import org.apache.tools.ant.DirectoryScanner;
38 import org.apache.tools.ant.Task;
39 import org.apache.tools.ant.types.FileSet;
40 import org.xml.sax.Attributes JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43 import org.xml.sax.helpers.DefaultHandler JavaDoc;
44
45 /**
46  * Task to scan all XML layers in a NB installation
47  * and report on which modules registers which files.
48  * @author Jesse Glick
49  */

50 public class LayerIndex extends Task {
51
52     public LayerIndex() {}
53
54     List JavaDoc<FileSet> filesets = new ArrayList JavaDoc<FileSet>();
55     public void addConfiguredModules(FileSet fs) {
56         filesets.add(fs);
57     }
58
59     private File JavaDoc output;
60     public void setOutput(File JavaDoc f) {
61         output = f;
62     }
63
64     @Override JavaDoc
65     public void execute() throws BuildException {
66         if (filesets.isEmpty()) {
67             throw new BuildException();
68         }
69         SortedMap JavaDoc<String JavaDoc,String JavaDoc> files = new TreeMap JavaDoc<String JavaDoc,String JavaDoc>(); // layer path -> cnb
70
for (FileSet fs : filesets) {
71             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
72             File JavaDoc basedir = ds.getBasedir();
73             for (String JavaDoc path : ds.getIncludedFiles()) {
74                 File JavaDoc jar = new File JavaDoc(basedir, path);
75                 try {
76                     JarFile JavaDoc jf = new JarFile JavaDoc(jar);
77                     try {
78                         Manifest JavaDoc mf = jf.getManifest();
79                         if (mf == null) {
80                             continue;
81                         }
82                         String JavaDoc modname = mf.getMainAttributes().getValue("OpenIDE-Module");
83                         if (modname == null) {
84                             continue;
85                         }
86                         String JavaDoc cnb = modname.replaceFirst("/\\d+$", "");
87                         String JavaDoc layer = mf.getMainAttributes().getValue("OpenIDE-Module-Layer");
88                         if (layer == null) {
89                             continue;
90                         }
91                         parse(jf.getInputStream(jf.getEntry(layer)), files, cnb);
92                     } finally {
93                         jf.close();
94                     }
95                 } catch (Exception JavaDoc x) {
96                     throw new BuildException("Reading " + jar + ": " + x, x, getLocation());
97                 }
98             }
99         }
100         int maxlength = 0;
101         for (String JavaDoc cnb : files.values()) {
102             if (cnb != null) {
103                 maxlength = Math.max(maxlength, cnb.length());
104             }
105         }
106         try {
107             PrintWriter JavaDoc pw = output != null ? new PrintWriter JavaDoc(output) : null;
108             for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> entry : files.entrySet()) {
109                 String JavaDoc path = entry.getKey();
110                 String JavaDoc cnb = entry.getValue();
111                 if (cnb == null) {
112                     cnb = "";
113                 }
114                 String JavaDoc line = String.format("%-" + maxlength + "s %s", cnb, path);
115                 if (pw != null) {
116                     pw.println(line);
117                 } else {
118                     log(line);
119                 }
120             }
121             if (pw != null) {
122                 pw.close();
123             }
124         } catch (FileNotFoundException JavaDoc x) {
125             throw new BuildException(x, getLocation());
126         }
127         if (output != null) {
128             log(output + ": layer index written");
129         }
130     }
131
132     private void parse(InputStream JavaDoc is, final Map JavaDoc<String JavaDoc,String JavaDoc> files, final String JavaDoc cnb) throws Exception JavaDoc {
133         SAXParserFactory JavaDoc f = SAXParserFactory.newInstance();
134         f.setValidating(false);
135         f.setNamespaceAware(false);
136         f.newSAXParser().parse(is, new DefaultHandler JavaDoc() {
137             String JavaDoc prefix = "";
138             void register(String JavaDoc path) {
139                 if (files.containsKey(path)) {
140                     files.put(path, null); // >1 owner
141
} else {
142                     files.put(path, cnb);
143                 }
144             }
145             @Override JavaDoc
146             public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
147                 if (qName.equals("folder")) {
148                     String JavaDoc n = attributes.getValue("name");
149                     prefix += n + "/";
150                     register(prefix);
151                 } else if (qName.equals("file")) {
152                     String JavaDoc n = attributes.getValue("name");
153                     register(prefix + n);
154                 }
155             }
156             @Override JavaDoc
157             public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
158                 if (qName.equals("folder")) {
159                     prefix = prefix.replaceFirst("[^/]+/$", "");
160                 }
161             }
162             @Override JavaDoc
163             public InputSource JavaDoc resolveEntity(String JavaDoc pub, String JavaDoc sys) throws IOException JavaDoc, SAXException JavaDoc {
164                 return new InputSource JavaDoc(new StringReader JavaDoc(""));
165             }
166         });
167     }
168
169 }
170
Popular Tags