KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportmanager > ReportSourceDefinitionVersion


1 package com.calipso.reportgenerator.reportmanager;
2
3 import org.apache.commons.vfs.FileObject;
4
5 import org.w3c.dom.Document JavaDoc;
6 import org.w3c.dom.Node JavaDoc;
7 import org.w3c.dom.NodeList JavaDoc;
8 import org.w3c.dom.NamedNodeMap JavaDoc;
9
10 import java.io.*;
11 import java.util.Collection JavaDoc;
12 import java.util.Vector JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
16 import javax.xml.parsers.DocumentBuilder JavaDoc;
17 import javax.xml.transform.Transformer JavaDoc;
18 import javax.xml.transform.TransformerFactory JavaDoc;
19 import javax.xml.transform.Result JavaDoc;
20 import javax.xml.transform.stream.StreamResult JavaDoc;
21 import javax.xml.transform.dom.DOMSource JavaDoc;
22
23 import com.calipso.reportgenerator.common.InfoException;
24 import com.calipso.reportgenerator.common.LanguageTraslator;
25
26 /**
27  *
28  * User: jbassino
29  * Date: 30/09/2004
30  * Time: 15:02:33
31  *
32  */

33 public class ReportSourceDefinitionVersion {
34
35   public static void validateVersion(FileObject fileObject) throws InfoException{
36     try{
37       File file = new File(fileObject.getName().getPath());
38       if(isOldVersion(file)){
39         transformToNewVersion(file);
40       }
41     }catch (Exception JavaDoc e){
42       throw new InfoException(LanguageTraslator.traslate(""), e);
43     }
44   }
45
46   public static void transformToNewVersion(File file) throws Exception JavaDoc{
47     Node JavaDoc docRoot = getDocRoot(file);
48     Collection JavaDoc collection = getDimensionElements(docRoot);
49     eliminateAllElements(collection, "QUERYCONVERTTOSTRINGPATTERN");
50     writeFile(file, docRoot);
51   }
52
53   private static void writeFile(File file, Node JavaDoc docRoot) throws InfoException{
54     Document JavaDoc document = docRoot.getOwnerDocument();
55     String JavaDoc encoding = document.getDocumentElement().getAttribute("encoding");
56     try{
57       TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
58       Transformer JavaDoc transformer = tFactory.newTransformer();
59       DOMSource JavaDoc source = new DOMSource JavaDoc(document);
60
61       Result JavaDoc result = new StreamResult JavaDoc(new FileOutputStream(file));
62       transformer.transform(source, result);
63       System.out.println("ResportSourceDefinition upgraded");
64       /*if (document.getDoctype() != null){
65         String systemValue = (new File(document.getDoctype().getSystemId())).getName();
66         transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue);;
67       }*/

68     }catch (Exception JavaDoc e){
69       throw new InfoException(LanguageTraslator.traslate("376"), e);
70     }
71   }
72
73   private static void eliminateAllElements(Collection JavaDoc collection, String JavaDoc elementToEliminate) {
74     for (Iterator JavaDoc iterator = collection.iterator(); iterator.hasNext();) {
75       Node JavaDoc node = (Node JavaDoc) iterator.next();
76       eliminateElement(node, elementToEliminate);
77     }
78   }
79
80   private static void eliminateElement(Node JavaDoc node, String JavaDoc elementToEliminate) {
81     if(node.hasAttributes()){
82       NamedNodeMap JavaDoc attrs = node.getAttributes();
83       for(int i=0; i < attrs.getLength(); i++){
84         if(attrs.item(i).getNodeName().equalsIgnoreCase(elementToEliminate)){
85           attrs.removeNamedItem(attrs.item(i).getNodeName());
86         }
87       }
88     }
89   }
90
91   private static Collection JavaDoc getDimensionElements(Node JavaDoc docRoot) throws InfoException{
92     Collection JavaDoc result = new Vector JavaDoc();
93     NodeList JavaDoc nodes = docRoot.getChildNodes();
94     Node JavaDoc dimensions = null;
95     for(int i=0; i < nodes.getLength(); i++){
96       dimensions = nodes.item(i);
97       if(dimensions.getNodeName().equalsIgnoreCase("DimensionSourceDefinitions")){
98         break;
99       }
100     }
101     if(dimensions!=null){
102       nodes = dimensions.getChildNodes();
103       for(int i=0; i < nodes.getLength(); i++){
104         Node JavaDoc dimension = nodes.item(i);
105         result.add(dimension);
106       }
107     }else{
108       throw new InfoException(LanguageTraslator.traslate("375"));
109     }
110     return result;
111   }
112
113   public static Node JavaDoc getDocRoot(File file) throws Exception JavaDoc{
114     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
115     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
116     Document JavaDoc document = builder.parse(file);
117     return document.getDocumentElement();
118   }
119
120   public static boolean isOldVersion(File file) throws IOException{
121     StringBuffer JavaDoc buffer = readFile(file);
122     String JavaDoc s = buffer.toString();
123     s = s.toUpperCase();
124     return (s.indexOf("QUERYCONVERTTOSTRINGPATTERN") > 0);
125   }
126
127   /*private static StringBuffer readFile(File file, FileObject fileObject) throws Exception{
128     long size = fileObject.getContent().getSize();
129     FileInputStream stream = new FileInputStream(file);
130     FileReader reader = new FileReader(file);
131     FileInputStream input = new FileInputStream(file);
132     FileWriter writer = new FileWriter(file);
133     DataInputStream data = new DataInputStream(input);
134     data.readUTF();
135     return null;
136   }*/

137
138
139   /**
140    * Cargar el archivo en un StringBuffer, leyendolo linea a linea
141    * @param file archivo a leer
142    * @return StringBuffer el texto del archivo
143    * @throws IOException
144    */

145   public static StringBuffer JavaDoc readFile(File file) throws IOException {
146     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
147     FileInputStream fis = new FileInputStream(file);
148     InputStreamReader isr = new InputStreamReader(fis);
149     BufferedReader in = new BufferedReader(isr);
150     String JavaDoc s;
151     while((s = in.readLine()) != null) {
152       sb.append(s);
153     }
154     in.close();
155     return sb;
156   }
157
158 }
159
Popular Tags