KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > MakeZipProto


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.util;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.BufferedWriter JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.FileNotFoundException JavaDoc;
33
34 import java.util.StringTokenizer JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
38 import javax.xml.parsers.DocumentBuilder JavaDoc;
39 import javax.xml.parsers.ParserConfigurationException JavaDoc;
40
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.w3c.dom.NamedNodeMap JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
47
48 /**
49  * This class is a utility intended to ease the conversion of Solaris package
50  * definitions defined in a prototype_com file into a file listing needed
51  * to drive the creation of a zip file. Each Solaris package (e.g.
52  * SUNWasuo) contains a prototype_com file that defines the files that
53  * make up that package. There are corresponding prototype_com.win2k and
54  * prototype_com.unix files the define the file listing for the zip files.
55  * These zip files are used for the evaluation installer on Solaris and
56  * the mainstream and evaluation installers on Win2K. This utility helps
57  * create the prototype_com.win2k and prototype_com.uniz files from the
58  * prototype_com file. For and example see iplanet/ias/server/src/pkg/SUNWasu.
59  *
60  * WARNING: This file is temporarily checked in here and really belongs
61  * more as part of the installer/build system; however, the installer is
62  * compiled with an older JDK version, and will not compile this file
63  * due to replaceAll.
64  */

65 public class MakeZipProto {
66
67     public static String JavaDoc parseFile(String JavaDoc line) {
68         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(line);
69         tok.nextToken();
70         tok.nextToken();
71         String JavaDoc result = tok.nextToken().replaceAll("\\$ASINSTDIR/", "");
72         int pos = result.indexOf("=");
73         if (pos > 0) {
74             return result.substring(pos + 1);
75         }
76         return result;
77     }
78
79     public static String JavaDoc parse(String JavaDoc line) {
80         String JavaDoc result = line.trim();
81         //skip empty lines
82
if (result.length() == 0) {
83             return null;
84         }
85         //skip comments
86
if (result.startsWith("#")) {
87             return null;
88         }
89         //skip directories
90
if (result.startsWith("d ")) {
91             return null;
92         }
93         //skip included packaging files
94
if (result.startsWith("i ")) {
95             return null;
96         }
97         //process normal files
98
if (result.startsWith("f ")) {
99             return parseFile(result);
100         }
101         return result;
102     }
103
104     protected static void writeOutputFile(String JavaDoc out, ArrayList JavaDoc fileList)
105         throws FileNotFoundException JavaDoc, IOException JavaDoc
106     {
107         BufferedWriter JavaDoc writer = null;
108         try {
109             writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(out, false));
110             for (int i = 0; i < fileList.size(); i++) {
111                 writer.write((String JavaDoc)fileList.get(i));
112                 writer.newLine();
113             }
114         } finally {
115             try {
116                 if (writer != null) {
117                     writer.close();
118                 }
119             } catch (Exception JavaDoc ex) {
120                 ex.printStackTrace();
121             }
122         }
123     }
124
125     protected static ArrayList JavaDoc readInputFile(String JavaDoc in)
126         throws FileNotFoundException JavaDoc, IOException JavaDoc
127     {
128         ArrayList JavaDoc result = new ArrayList JavaDoc();
129         BufferedReader JavaDoc reader = null;
130         try {
131             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(in));
132             String JavaDoc line = null;
133             String JavaDoc newLine = null;
134             while (true) {
135                 line = reader.readLine();
136                 if (line == null) {
137                     break;
138                 }
139                 newLine = parse(line);
140                 if (newLine != null) {
141                     result.add(newLine);
142                 }
143             }
144             return result;
145         } finally {
146             try {
147                 if (reader != null) {
148                     reader.close();
149                 }
150             } catch (Exception JavaDoc ex) {
151                 ex.printStackTrace();
152             }
153         }
154     }
155
156     protected static void filesetElement(Node JavaDoc filesetNode,
157         ArrayList JavaDoc fileList)
158     {
159         NamedNodeMap JavaDoc attributes = filesetNode.getAttributes();
160         Node JavaDoc includes = attributes.getNamedItem("includes");
161         if (includes != null) {
162             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(includes.getNodeValue());
163             while (tok.hasMoreTokens()) {
164                 fileList.add(tok.nextToken());
165             }
166         }
167         ArrayList JavaDoc removeList = new ArrayList JavaDoc();
168         Node JavaDoc excludes = attributes.getNamedItem("excludes");
169         if (excludes != null) {
170             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(excludes.getNodeValue());
171             while (tok.hasMoreTokens()) {
172                 removeList.add(tok.nextToken());
173             }
174         }
175         for (int i = 0; i < fileList.size(); i++) {
176             if (removeList.contains(fileList.get(i))) {
177                 fileList.remove(i--);
178             }
179         }
180     }
181     
182     protected static void zipfileElement(Node JavaDoc zipfileNode)
183         throws FileNotFoundException JavaDoc, IOException JavaDoc
184     {
185         NamedNodeMap JavaDoc attributes = zipfileNode.getAttributes();
186         String JavaDoc inputFile = attributes.getNamedItem("input").getNodeValue();
187         System.out.println("inputFile = " + inputFile);
188         String JavaDoc outputFile = attributes.getNamedItem("output").getNodeValue();
189         System.out.println("output = " + outputFile);
190         ArrayList JavaDoc fileList = readInputFile(inputFile);
191         for (int i = 0; i < fileList.size(); i++) {
192             System.out.println("fileList before " + i + " = " +
193                fileList.get(i));
194         }
195         NodeList JavaDoc children = zipfileNode.getChildNodes();
196         for (int j = 0; j < children.getLength(); j++) {
197             Node JavaDoc childNode = children.item(j);
198             if (childNode.getNodeName().equals("fileset")) {
199                 filesetElement(childNode, fileList);
200             }
201         }
202         for (int i = 0; i < fileList.size(); i++) {
203             System.out.println("fileList after " + i + " = " +
204                fileList.get(i));
205         }
206         writeOutputFile(outputFile, fileList);
207     }
208
209     protected static void parseXML(String JavaDoc fileName)
210         throws FileNotFoundException JavaDoc, SAXException JavaDoc, IOException JavaDoc,
211         ParserConfigurationException JavaDoc
212     {
213         DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
214         FileInputStream JavaDoc source = new FileInputStream JavaDoc(fileName);
215         Document JavaDoc document = builder.parse(source);
216         NodeList JavaDoc nodeList = document.getElementsByTagName("zipfile");
217         if (nodeList != null) {
218             for (int i = 0; i < nodeList.getLength(); i++) {
219                 Node JavaDoc node = nodeList.item(i);
220                 zipfileElement(node);
221             }
222         }
223     }
224     
225     public static void main(String JavaDoc[] args) {
226         if (args.length != 1) {
227             System.out.println("usage: MakeZipProto xmlFile");
228         } else {
229             String JavaDoc xmlFile = args[0];
230             try {
231                 parseXML(xmlFile);
232                 //translate(inFile, outFile);
233
} catch (Exception JavaDoc ex) {
234                 ex.printStackTrace();
235             }
236         }
237     }
238 }
239
Popular Tags