001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.engine.util;
034:
035: import org.libresource.so6.core.command.Command;
036:
037: import org.xml.sax.Attributes;
038: import org.xml.sax.SAXException;
039: import org.xml.sax.helpers.DefaultHandler;
040:
041: import java.io.File;
042: import java.io.FileOutputStream;
043: import java.io.FileWriter;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.UnsupportedEncodingException;
047:
048: import java.util.ArrayList;
049: import java.util.Arrays;
050: import java.util.Hashtable;
051: import java.util.Iterator;
052: import java.util.Properties;
053:
054: import javax.xml.parsers.SAXParser;
055: import javax.xml.parsers.SAXParserFactory;
056:
057: /**
058: * @author smack
059: */
060: public class FileBrowserPatchHandler {
061: public static final String ATTACH_PROP_FILE = "attach.properties";
062: private String baseWorkingDir;
063:
064: public FileBrowserPatchHandler(String baseWorkingDir) {
065: this .baseWorkingDir = baseWorkingDir;
066: }
067:
068: public void exportAttachementWithProperties(InputStream patch)
069: throws Exception {
070: SAXParserFactory factory = SAXParserFactory.newInstance();
071: SAXParser saxParser = factory.newSAXParser();
072: saxParser.parse(patch, new PropertyFileBrowser(baseWorkingDir));
073: }
074:
075: public void exportAttachementWithHtmlIndex(boolean listOpName,
076: InputStream patch) throws Exception {
077: SAXParserFactory factory = SAXParserFactory.newInstance();
078: SAXParser saxParser = factory.newSAXParser();
079: saxParser.parse(patch, new HtmlFileBrowser(baseWorkingDir,
080: listOpName));
081: }
082:
083: public void exportAttachementWithProperties(File patch)
084: throws Exception {
085: SAXParserFactory factory = SAXParserFactory.newInstance();
086: SAXParser saxParser = factory.newSAXParser();
087: saxParser.parse(patch, new PropertyFileBrowser(baseWorkingDir));
088: }
089:
090: public void exportAttachementWithHtmlIndex(boolean listOpName,
091: File patch) throws Exception {
092: SAXParserFactory factory = SAXParserFactory.newInstance();
093: SAXParser saxParser = factory.newSAXParser();
094: saxParser.parse(patch, new HtmlFileBrowser(baseWorkingDir,
095: listOpName));
096: }
097:
098: public static void main(String[] args) throws Exception {
099: SAXParserFactory factory = SAXParserFactory.newInstance();
100: SAXParser saxParser = factory.newSAXParser();
101: FileBrowserPatchHandler patchBrowser = new FileBrowserPatchHandler(
102: args[1]);
103:
104: if ((args.length == 3) && args[2].equals("html")) {
105: patchBrowser.exportAttachementWithHtmlIndex(true, new File(
106: args[0]));
107: } else {
108: patchBrowser.exportAttachementWithProperties(new File(
109: args[0]));
110: }
111: }
112:
113: public class PropertyFileBrowser extends BrowsePatchHandler {
114: public PropertyFileBrowser(String workingDir) {
115: super (workingDir);
116: }
117:
118: public void endDocument() throws SAXException {
119: // build html web page
120: try {
121: Properties props = new Properties();
122: File f = new File(baseWorkingDir, ATTACH_PROP_FILE);
123: FileOutputStream fos = new FileOutputStream(f);
124: props.putAll(filesAttachement);
125: props.store(fos, "Liste of the file attachement");
126: fos.close();
127: } catch (Exception e) {
128: throw new SAXException(e);
129: }
130: }
131: }
132:
133: public class HtmlFileBrowser extends BrowsePatchHandler {
134: private boolean listOp;
135:
136: public HtmlFileBrowser(String workingDir, boolean listOp) {
137: super (workingDir);
138: this .listOp = listOp;
139: }
140:
141: public void endDocument() throws SAXException {
142: // build html web page
143: try {
144: File f = new File(baseWorkingDir, "index.html");
145: FileWriter fw = new FileWriter(f);
146: fw.write("<html><body><table>");
147:
148: int index = 0;
149: Arrays.sort(pathList.toArray());
150:
151: for (Iterator i = pathList.iterator(); i.hasNext();) {
152: String localPath = (String) i.next();
153: fw.write("<tr bgcolor=\"#"
154: + (((index++ % 2) == 0) ? "ccccee"
155: : "eeeeee") + "\"><td>" + localPath
156: + "</td><td>");
157:
158: if (filesAttachement.get(localPath) != null) {
159: fw.write("<a href=\""
160: + filesAttachement.get(localPath)
161: + "\">Download</a>");
162: }
163:
164: fw.write("</td></tr>");
165:
166: if (listOp) {
167: fw.write("<tr><td><ul>");
168:
169: for (Iterator j = ((ArrayList) filesIndex
170: .get(localPath)).iterator(); j
171: .hasNext();) {
172: fw.write("<li>");
173: fw.write((String) j.next());
174: fw.write("</li>");
175: }
176:
177: fw.write("</ul></td><td></td></tr>");
178: }
179: }
180:
181: fw.write("</table></body></html>");
182: fw.close();
183: } catch (Exception e) {
184: throw new SAXException(e);
185: }
186: }
187: }
188:
189: abstract class BrowsePatchHandler extends DefaultHandler {
190: // interesting tag name
191: private final static String INFO_TAG = "name begin end class path comment";
192: private FileOutputStream fos;
193: protected File baseWorkingDir;
194: private String tag;
195: private StringBuffer buffer;
196: private String className;
197: private String path;
198:
199: // information to retreive
200: private long fromTicket = -1;
201: private long toTicket = -1;
202: private String wsName;
203: private String comment = "";
204: protected Hashtable filesIndex;
205: protected Hashtable filesAttachement;
206: protected ArrayList pathList;
207:
208: public BrowsePatchHandler(String baseWorkingDir) {
209: this .filesIndex = new Hashtable();
210: this .filesAttachement = new Hashtable();
211: this .baseWorkingDir = new File(baseWorkingDir);
212: this .pathList = new ArrayList();
213: }
214:
215: public long getFromTicket() {
216: return fromTicket;
217: }
218:
219: public long getToTicket() {
220: return toTicket;
221: }
222:
223: public String getWsName() {
224: return wsName;
225: }
226:
227: public String getComment() {
228: return comment;
229: }
230:
231: public Hashtable getFileIndex() {
232: return filesIndex;
233: }
234:
235: public void characters(char[] buff, int offset, int len)
236: throws SAXException {
237: if (tag.equals(Command.ATTACHEMENT)) {
238: buffer.append(buff, offset, len);
239:
240: int pos = -1;
241:
242: while ((pos = buffer.indexOf("\n")) != -1) {
243: while ((pos = buffer.indexOf("\n")) != -1) {
244: buffer.deleteCharAt(pos);
245: }
246:
247: int nbCharToDecode = (buffer.length() / 4);
248:
249: try {
250: fos.write(Base64.decode(buffer.substring(0,
251: nbCharToDecode * 4)));
252: buffer.delete(0, nbCharToDecode * 4);
253: } catch (IOException e) {
254: e.printStackTrace();
255: throw new RuntimeException(e);
256: } catch (RuntimeException e) {
257: e.printStackTrace();
258: throw e;
259: }
260: }
261: } else if (INFO_TAG.indexOf(tag) != -1) {
262: buffer.append(buff, offset, len);
263: }
264: }
265:
266: public void endElement(String namespaceuri, String sname,
267: String qname) throws SAXException {
268: if (qname.equals(Command.ATTACHEMENT)) {
269: try {
270: fos.write(Base64.decode(buffer.toString()));
271: fos.flush();
272: fos.close();
273: } catch (IOException e) {
274: throw new RuntimeException(e);
275: }
276: }
277:
278: if (qname.equals("name")) {
279: try {
280: wsName = new String(Base64
281: .decode(buffer.toString()), "UTF-8");
282: } catch (UnsupportedEncodingException e) {
283: throw new RuntimeException(
284: "UTF-8 encoding not supported");
285: }
286: }
287:
288: if (qname.equals("begin")) {
289: fromTicket = Long.parseLong(buffer.toString());
290: }
291:
292: if (qname.equals("end")) {
293: toTicket = Long.parseLong(buffer.toString());
294: }
295:
296: if (qname.equals("class")) {
297: className = buffer.toString();
298: }
299:
300: if (qname.equals("comment")) {
301: try {
302: comment = new String(Base64.decode(buffer
303: .toString()), "UTF-8");
304: } catch (UnsupportedEncodingException e) {
305: throw new RuntimeException(
306: "UTF-8 encoding not supported");
307: }
308: }
309:
310: if (qname.equals("path")) {
311: try {
312: path = new String(Base64.decode(buffer.toString()),
313: "UTF-8");
314: } catch (UnsupportedEncodingException e) {
315: throw new RuntimeException(
316: "UTF-8 encoding not supported");
317: }
318:
319: if (!pathList.contains(path)) {
320: pathList.add(path);
321: }
322:
323: ArrayList opList = (ArrayList) filesIndex.get(path);
324:
325: if (opList != null) {
326: opList.add(className);
327: } else {
328: opList = new ArrayList();
329: opList.add(className);
330: filesIndex.put(path, opList);
331: }
332: }
333: }
334:
335: public void startElement(String namespaceuri, String sname,
336: String qname, Attributes attr) throws SAXException {
337: tag = qname;
338: buffer = new StringBuffer();
339:
340: if (qname.equals(Command.ATTACHEMENT)) {
341: try {
342: File f = File.createTempFile("attach", null,
343: baseWorkingDir);
344: fos = new FileOutputStream(f);
345: filesAttachement.put(path, f.getName());
346: } catch (IOException e) {
347: throw new RuntimeException(e);
348: }
349: }
350: }
351: }
352: }
|