Changeset 26457

Show
Ignore:
Timestamp:
14.11.2012 14:04:21 (7 years ago)
Author:
sjm84
Message:

Fixed the debug transformation so that it correctly reads files in UTF-8

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XMLTransformer.java

    r25727 r26457  
    1919package org.greenstone.gsdl3.util; 
    2020 
    21 import org.greenstone.util.GlobalProperties; 
    22  
    23 // XML classes 
     21import java.io.BufferedReader; 
     22import java.io.File; 
     23import java.io.FileInputStream; 
     24import java.io.FileNotFoundException; 
     25import java.io.FileWriter; 
     26import java.io.InputStreamReader; 
     27import java.io.StringReader; 
     28import java.io.StringWriter; 
     29import java.io.UnsupportedEncodingException; 
     30import java.util.HashMap; 
     31import java.util.Iterator; 
     32import java.util.Map; 
     33import java.util.Set; 
     34 
     35import javax.xml.parsers.DocumentBuilderFactory; 
     36import javax.xml.transform.ErrorListener; 
     37import javax.xml.transform.Source; 
    2438import javax.xml.transform.Transformer; 
    25 import javax.xml.transform.TransformerFactory; 
    2639import javax.xml.transform.TransformerConfigurationException; 
    2740import javax.xml.transform.TransformerException; 
    28 import javax.xml.transform.ErrorListener; 
    29  
     41import javax.xml.transform.TransformerFactory; 
     42import javax.xml.transform.dom.DOMResult; 
     43import javax.xml.transform.dom.DOMSource; 
     44import javax.xml.transform.stream.StreamResult; 
    3045import javax.xml.transform.stream.StreamSource; 
    31 import javax.xml.transform.dom.DOMSource; 
    32 import javax.xml.transform.Source; 
    33 import javax.xml.transform.stream.StreamResult; 
    34 import javax.xml.transform.dom.DOMResult; 
    35  
    36 import javax.xml.parsers.DocumentBuilderFactory; 
    37 import javax.xml.parsers.DocumentBuilder; 
     46 
     47import org.apache.log4j.Logger; 
     48import org.greenstone.util.GlobalProperties; 
     49import org.w3c.dom.Document; 
    3850import org.w3c.dom.Element; 
    39 import org.w3c.dom.Document; 
    40  
    4151import org.w3c.dom.Node; 
    42 import org.w3c.dom.NodeList; 
    43  
    44 // other java classes 
    45 import java.io.StringReader; 
    46 import java.io.StringWriter; 
    47 import java.io.BufferedReader; 
    48 import java.io.FileReader; 
    49 import java.io.FileWriter; 
    50 import java.io.File; 
    51 import java.util.HashMap; 
    52 import java.util.Set; 
    53 import java.util.Map; 
    54 import java.util.Iterator; 
    55  
    56 import org.apache.xml.utils.DefaultErrorHandler; 
    57  
    58 import org.apache.log4j.*; 
    5952 
    6053/** 
     
    288281            TransformErrorListener transformerErrorListener = (TransformErrorListener) this.t_factory.getErrorListener(); 
    289282            transformerErrorListener.setStylesheet(stylesheet); 
    290             Transformer transformer = this.t_factory.newTransformer(new StreamSource(stylesheet)); 
     283            Transformer transformer = this.t_factory.newTransformer(new StreamSource(new InputStreamReader(new FileInputStream(stylesheet), "UTF-8"))); 
    291284            DOMResult result = (docDocType == null) ? new DOMResult() : new DOMResult(docDocType); 
    292             StreamSource streamSource = new StreamSource(source); 
     285            StreamSource streamSource = new StreamSource(new InputStreamReader(new FileInputStream(source), "UTF-8")); 
    293286 
    294287            transformer.setErrorListener(new TransformErrorListener(stylesheet, streamSource)); 
     
    304297        { 
    305298            return transformError("XMLTransformer.transform(File, File)" + "\ncouldn't transform the source for files\n" + stylesheet + "\n" + source, e); 
     299        } 
     300        catch (UnsupportedEncodingException e) 
     301        { 
     302            return transformError("XMLTransformer.transform(File, File)" + "\ncouldn't read file due to an unsupported encoding\n" + stylesheet + "\n" + source, e); 
     303        } 
     304        catch (FileNotFoundException e) 
     305        { 
     306            return transformError("XMLTransformer.transform(File, File)" + "\ncouldn't find the file specified\n" + stylesheet + "\n" + source, e); 
    306307        } 
    307308    } 
     
    310311    // this method prints the exception to the tomcat window (system.err) and the greenstone log and then returns 
    311312    // an xhtml error page that is constructed from it. 
    312     protected Node transformError(String heading, TransformerException e) 
     313    protected Node transformError(String heading, Exception e) 
    313314    { 
    314315        String message = heading + "\n" + e.getMessage(); 
    315316        logger.error(heading + ": " + e.getMessage()); 
    316317 
    317         String location = e.getLocationAsString(); 
    318         if (location != null) 
    319         { 
    320             logger.error(location); 
    321             message = message + "\n" + location; 
     318        if (e instanceof TransformerException) 
     319        { 
     320            String location = ((TransformerException) e).getLocationAsString(); 
     321            if (location != null) 
     322            { 
     323                logger.error(location); 
     324                message = message + "\n" + location; 
     325            } 
    322326        } 
    323327        System.err.println("****\n" + message + "\n****"); 
     
    552556            File styleFile = new File(webLogsTmpFolder + File.separator + "stylesheet" + XMLTransformer.debugFileCount + ".xml"); 
    553557            File sourceFile = new File(webLogsTmpFolder + File.separator + "source" + XMLTransformer.debugFileCount + ".xml"); 
    554  
    555558            try 
    556559            { 
     
    613616                XMLTransformer.this.transform(styleFile, sourceFile); // calls the File, File version, so debugAsFile will be false      
    614617            } 
    615  
    616618            else 
    617619            { // ErrorListener was set on a TransformerFactory object