001: package com.calipso.reportgenerator.reportmanager;
002:
003: import org.apache.commons.vfs.FileObject;
004:
005: import org.w3c.dom.Document;
006: import org.w3c.dom.Node;
007: import org.w3c.dom.NodeList;
008: import org.w3c.dom.NamedNodeMap;
009:
010: import java.io.*;
011: import java.util.Collection;
012: import java.util.Vector;
013: import java.util.Iterator;
014:
015: import javax.xml.parsers.DocumentBuilderFactory;
016: import javax.xml.parsers.DocumentBuilder;
017: import javax.xml.transform.Transformer;
018: import javax.xml.transform.TransformerFactory;
019: import javax.xml.transform.Result;
020: import javax.xml.transform.stream.StreamResult;
021: import javax.xml.transform.dom.DOMSource;
022:
023: import com.calipso.reportgenerator.common.InfoException;
024: import com.calipso.reportgenerator.common.LanguageTraslator;
025:
026: /**
027: *
028: * User: jbassino
029: * Date: 30/09/2004
030: * Time: 15:02:33
031: *
032: */
033: public class ReportSourceDefinitionVersion {
034:
035: public static void validateVersion(FileObject fileObject)
036: throws InfoException {
037: try {
038: File file = new File(fileObject.getName().getPath());
039: if (isOldVersion(file)) {
040: transformToNewVersion(file);
041: }
042: } catch (Exception e) {
043: throw new InfoException(LanguageTraslator.traslate(""), e);
044: }
045: }
046:
047: public static void transformToNewVersion(File file)
048: throws Exception {
049: Node docRoot = getDocRoot(file);
050: Collection collection = getDimensionElements(docRoot);
051: eliminateAllElements(collection, "QUERYCONVERTTOSTRINGPATTERN");
052: writeFile(file, docRoot);
053: }
054:
055: private static void writeFile(File file, Node docRoot)
056: throws InfoException {
057: Document document = docRoot.getOwnerDocument();
058: String encoding = document.getDocumentElement().getAttribute(
059: "encoding");
060: try {
061: TransformerFactory tFactory = TransformerFactory
062: .newInstance();
063: Transformer transformer = tFactory.newTransformer();
064: DOMSource source = new DOMSource(document);
065:
066: Result result = new StreamResult(new FileOutputStream(file));
067: transformer.transform(source, result);
068: System.out.println("ResportSourceDefinition upgraded");
069: /*if (document.getDoctype() != null){
070: String systemValue = (new File(document.getDoctype().getSystemId())).getName();
071: transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue);;
072: }*/
073: } catch (Exception e) {
074: throw new InfoException(LanguageTraslator.traslate("376"),
075: e);
076: }
077: }
078:
079: private static void eliminateAllElements(Collection collection,
080: String elementToEliminate) {
081: for (Iterator iterator = collection.iterator(); iterator
082: .hasNext();) {
083: Node node = (Node) iterator.next();
084: eliminateElement(node, elementToEliminate);
085: }
086: }
087:
088: private static void eliminateElement(Node node,
089: String elementToEliminate) {
090: if (node.hasAttributes()) {
091: NamedNodeMap attrs = node.getAttributes();
092: for (int i = 0; i < attrs.getLength(); i++) {
093: if (attrs.item(i).getNodeName().equalsIgnoreCase(
094: elementToEliminate)) {
095: attrs.removeNamedItem(attrs.item(i).getNodeName());
096: }
097: }
098: }
099: }
100:
101: private static Collection getDimensionElements(Node docRoot)
102: throws InfoException {
103: Collection result = new Vector();
104: NodeList nodes = docRoot.getChildNodes();
105: Node dimensions = null;
106: for (int i = 0; i < nodes.getLength(); i++) {
107: dimensions = nodes.item(i);
108: if (dimensions.getNodeName().equalsIgnoreCase(
109: "DimensionSourceDefinitions")) {
110: break;
111: }
112: }
113: if (dimensions != null) {
114: nodes = dimensions.getChildNodes();
115: for (int i = 0; i < nodes.getLength(); i++) {
116: Node dimension = nodes.item(i);
117: result.add(dimension);
118: }
119: } else {
120: throw new InfoException(LanguageTraslator.traslate("375"));
121: }
122: return result;
123: }
124:
125: public static Node getDocRoot(File file) throws Exception {
126: DocumentBuilderFactory factory = DocumentBuilderFactory
127: .newInstance();
128: DocumentBuilder builder = factory.newDocumentBuilder();
129: Document document = builder.parse(file);
130: return document.getDocumentElement();
131: }
132:
133: public static boolean isOldVersion(File file) throws IOException {
134: StringBuffer buffer = readFile(file);
135: String s = buffer.toString();
136: s = s.toUpperCase();
137: return (s.indexOf("QUERYCONVERTTOSTRINGPATTERN") > 0);
138: }
139:
140: /*private static StringBuffer readFile(File file, FileObject fileObject) throws Exception{
141: long size = fileObject.getContent().getSize();
142: FileInputStream stream = new FileInputStream(file);
143: FileReader reader = new FileReader(file);
144: FileInputStream input = new FileInputStream(file);
145: FileWriter writer = new FileWriter(file);
146: DataInputStream data = new DataInputStream(input);
147: data.readUTF();
148: return null;
149: }*/
150:
151: /**
152: * Cargar el archivo en un StringBuffer, leyendolo linea a linea
153: * @param file archivo a leer
154: * @return StringBuffer el texto del archivo
155: * @throws IOException
156: */
157: public static StringBuffer readFile(File file) throws IOException {
158: StringBuffer sb = new StringBuffer();
159: FileInputStream fis = new FileInputStream(file);
160: InputStreamReader isr = new InputStreamReader(fis);
161: BufferedReader in = new BufferedReader(isr);
162: String s;
163: while ((s = in.readLine()) != null) {
164: sb.append(s);
165: }
166: in.close();
167: return sb;
168: }
169:
170: }
|