1 package com.calipso.reportgenerator.reportmanager; 2 3 import org.apache.commons.vfs.FileObject; 4 5 import org.w3c.dom.Document ; 6 import org.w3c.dom.Node ; 7 import org.w3c.dom.NodeList ; 8 import org.w3c.dom.NamedNodeMap ; 9 10 import java.io.*; 11 import java.util.Collection ; 12 import java.util.Vector ; 13 import java.util.Iterator ; 14 15 import javax.xml.parsers.DocumentBuilderFactory ; 16 import javax.xml.parsers.DocumentBuilder ; 17 import javax.xml.transform.Transformer ; 18 import javax.xml.transform.TransformerFactory ; 19 import javax.xml.transform.Result ; 20 import javax.xml.transform.stream.StreamResult ; 21 import javax.xml.transform.dom.DOMSource ; 22 23 import com.calipso.reportgenerator.common.InfoException; 24 import com.calipso.reportgenerator.common.LanguageTraslator; 25 26 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 e){ 42 throw new InfoException(LanguageTraslator.traslate(""), e); 43 } 44 } 45 46 public static void transformToNewVersion(File file) throws Exception { 47 Node docRoot = getDocRoot(file); 48 Collection collection = getDimensionElements(docRoot); 49 eliminateAllElements(collection, "QUERYCONVERTTOSTRINGPATTERN"); 50 writeFile(file, docRoot); 51 } 52 53 private static void writeFile(File file, Node docRoot) throws InfoException{ 54 Document document = docRoot.getOwnerDocument(); 55 String encoding = document.getDocumentElement().getAttribute("encoding"); 56 try{ 57 TransformerFactory tFactory = TransformerFactory.newInstance(); 58 Transformer transformer = tFactory.newTransformer(); 59 DOMSource source = new DOMSource (document); 60 61 Result result = new StreamResult (new FileOutputStream(file)); 62 transformer.transform(source, result); 63 System.out.println("ResportSourceDefinition upgraded"); 64 68 }catch (Exception e){ 69 throw new InfoException(LanguageTraslator.traslate("376"), e); 70 } 71 } 72 73 private static void eliminateAllElements(Collection collection, String elementToEliminate) { 74 for (Iterator iterator = collection.iterator(); iterator.hasNext();) { 75 Node node = (Node ) iterator.next(); 76 eliminateElement(node, elementToEliminate); 77 } 78 } 79 80 private static void eliminateElement(Node node, String elementToEliminate) { 81 if(node.hasAttributes()){ 82 NamedNodeMap 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 getDimensionElements(Node docRoot) throws InfoException{ 92 Collection result = new Vector (); 93 NodeList nodes = docRoot.getChildNodes(); 94 Node 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 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 getDocRoot(File file) throws Exception { 114 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 115 DocumentBuilder builder = factory.newDocumentBuilder(); 116 Document document = builder.parse(file); 117 return document.getDocumentElement(); 118 } 119 120 public static boolean isOldVersion(File file) throws IOException{ 121 StringBuffer buffer = readFile(file); 122 String s = buffer.toString(); 123 s = s.toUpperCase(); 124 return (s.indexOf("QUERYCONVERTTOSTRINGPATTERN") > 0); 125 } 126 127 137 138 139 145 public static StringBuffer readFile(File file) throws IOException { 146 StringBuffer sb = new StringBuffer (); 147 FileInputStream fis = new FileInputStream(file); 148 InputStreamReader isr = new InputStreamReader(fis); 149 BufferedReader in = new BufferedReader(isr); 150 String s; 151 while((s = in.readLine()) != null) { 152 sb.append(s); 153 } 154 in.close(); 155 return sb; 156 } 157 158 } 159 | Popular Tags |