1 23 24 package com.sun.enterprise.util; 25 26 import java.io.BufferedReader ; 27 import java.io.BufferedWriter ; 28 import java.io.FileReader ; 29 import java.io.FileWriter ; 30 import java.io.FileInputStream ; 31 import java.io.IOException ; 32 import java.io.FileNotFoundException ; 33 34 import java.util.StringTokenizer ; 35 import java.util.ArrayList ; 36 37 import javax.xml.parsers.DocumentBuilderFactory ; 38 import javax.xml.parsers.DocumentBuilder ; 39 import javax.xml.parsers.ParserConfigurationException ; 40 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NamedNodeMap ; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.NodeList ; 46 import org.xml.sax.SAXException ; 47 48 65 public class MakeZipProto { 66 67 public static String parseFile(String line) { 68 StringTokenizer tok = new StringTokenizer (line); 69 tok.nextToken(); 70 tok.nextToken(); 71 String 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 parse(String line) { 80 String result = line.trim(); 81 if (result.length() == 0) { 83 return null; 84 } 85 if (result.startsWith("#")) { 87 return null; 88 } 89 if (result.startsWith("d ")) { 91 return null; 92 } 93 if (result.startsWith("i ")) { 95 return null; 96 } 97 if (result.startsWith("f ")) { 99 return parseFile(result); 100 } 101 return result; 102 } 103 104 protected static void writeOutputFile(String out, ArrayList fileList) 105 throws FileNotFoundException , IOException 106 { 107 BufferedWriter writer = null; 108 try { 109 writer = new BufferedWriter (new FileWriter (out, false)); 110 for (int i = 0; i < fileList.size(); i++) { 111 writer.write((String )fileList.get(i)); 112 writer.newLine(); 113 } 114 } finally { 115 try { 116 if (writer != null) { 117 writer.close(); 118 } 119 } catch (Exception ex) { 120 ex.printStackTrace(); 121 } 122 } 123 } 124 125 protected static ArrayList readInputFile(String in) 126 throws FileNotFoundException , IOException 127 { 128 ArrayList result = new ArrayList (); 129 BufferedReader reader = null; 130 try { 131 reader = new BufferedReader (new FileReader (in)); 132 String line = null; 133 String 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 ex) { 151 ex.printStackTrace(); 152 } 153 } 154 } 155 156 protected static void filesetElement(Node filesetNode, 157 ArrayList fileList) 158 { 159 NamedNodeMap attributes = filesetNode.getAttributes(); 160 Node includes = attributes.getNamedItem("includes"); 161 if (includes != null) { 162 StringTokenizer tok = new StringTokenizer (includes.getNodeValue()); 163 while (tok.hasMoreTokens()) { 164 fileList.add(tok.nextToken()); 165 } 166 } 167 ArrayList removeList = new ArrayList (); 168 Node excludes = attributes.getNamedItem("excludes"); 169 if (excludes != null) { 170 StringTokenizer tok = new StringTokenizer (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 zipfileNode) 183 throws FileNotFoundException , IOException 184 { 185 NamedNodeMap attributes = zipfileNode.getAttributes(); 186 String inputFile = attributes.getNamedItem("input").getNodeValue(); 187 System.out.println("inputFile = " + inputFile); 188 String outputFile = attributes.getNamedItem("output").getNodeValue(); 189 System.out.println("output = " + outputFile); 190 ArrayList 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 children = zipfileNode.getChildNodes(); 196 for (int j = 0; j < children.getLength(); j++) { 197 Node 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 fileName) 210 throws FileNotFoundException , SAXException , IOException , 211 ParserConfigurationException 212 { 213 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 214 FileInputStream source = new FileInputStream (fileName); 215 Document document = builder.parse(source); 216 NodeList nodeList = document.getElementsByTagName("zipfile"); 217 if (nodeList != null) { 218 for (int i = 0; i < nodeList.getLength(); i++) { 219 Node node = nodeList.item(i); 220 zipfileElement(node); 221 } 222 } 223 } 224 225 public static void main(String [] args) { 226 if (args.length != 1) { 227 System.out.println("usage: MakeZipProto xmlFile"); 228 } else { 229 String xmlFile = args[0]; 230 try { 231 parseXML(xmlFile); 232 } catch (Exception ex) { 234 ex.printStackTrace(); 235 } 236 } 237 } 238 } 239 | Popular Tags |