Changeset 32491 for main

Show
Ignore:
Timestamp:
25.09.2018 13:03:50 (8 months ago)
Author:
kjdon
Message:

added a new transform method for 2 files where you can pass in the entityresolver. this will help the transform to locate dtds etc. eg for gberg coll, the code is looking in tomcat/bin folder for gutbook.dtd. don't know why this is, or why it has changed from before. Anyway, we need to use XMLReader and give it the entityresolver to locate the dtd (from the resources folder in the collection)

Files:
1 modified

Legend:

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

    r30782 r32491  
    5959import org.w3c.dom.Node; 
    6060 
     61import org.xml.sax.XMLReader; 
     62import org.xml.sax.EntityResolver; 
     63import org.xml.sax.InputSource; 
     64import org.xml.sax.SAXException; 
     65import javax.xml.transform.sax.SAXSource; 
     66import org.xml.sax.helpers.XMLReaderFactory; 
    6167/** 
    6268 * XMLTransformer - utility class for greenstone 
     
    6470 * transforms xml using xslt 
    6571 *  
    66  * @author Katherine Don 
    67  * @version $Revision$ 
    6872 */ 
    6973public class XMLTransformer 
     
    231235    protected Node transform(Document stylesheet, Document source, HashMap<String, Object> parameters, Document docDocType, Document resultNode) 
    232236    { 
     237              
    233238        try 
    234239        { 
     
    470475    } 
    471476 
     477  /** transform method for when the source doc may contain DTD references. eg in gberg collection - the files are kind of outside of greenstone. 
     478   * pass in an entity resolver, and it will be used by the XMLReader */ 
     479  public Node transform(File stylesheet, File source,  Document docDocType, EntityResolver er) 
     480  { 
     481        try 
     482        { 
     483          TransformErrorListener transformerErrorListener = (TransformErrorListener) this.t_factory.getErrorListener(); 
     484            transformerErrorListener.setStylesheet(stylesheet); 
     485            Transformer transformer = this.t_factory.newTransformer(new StreamSource(new InputStreamReader(new FileInputStream(stylesheet), "UTF-8"))); 
     486            DOMResult result =  (docDocType == null) ? new DOMResult() : new DOMResult(docDocType); 
     487 
     488            XMLReader reader = XMLReaderFactory.createXMLReader(); 
     489            reader.setEntityResolver(er); 
     490            SAXSource xmlSource = new SAXSource(reader, new InputSource(new InputStreamReader(new FileInputStream(source), "UTF-8"))); 
     491         
     492            transformer.setErrorListener(new TransformErrorListener(stylesheet, xmlSource));  
     493 
     494            transformer.transform(xmlSource, result);  
     495            return result.getNode().getFirstChild(); 
     496        } 
     497        catch (SAXException e) 
     498          { 
     499            return transformError("XMLTransformer.transform(File, File), couldn't create XMLReader for source\n"+ source, e); 
     500          } 
     501        catch (TransformerConfigurationException e) 
     502        { 
     503            return transformError("XMLTransformer.transform(File, File)" + "\ncouldn't create transformer object for files\n" + stylesheet + "\n" + source, e); 
     504        } 
     505        catch (TransformerException e) 
     506        { 
     507            return transformError("XMLTransformer.transform(File, File)" + "\ncouldn't transform the source for files\n" + stylesheet + "\n" + source, e); 
     508        } 
     509        catch (UnsupportedEncodingException e) 
     510        { 
     511            return transformError("XMLTransformer.transform(File, File)" + "\ncouldn't read file due to an unsupported encoding\n" + stylesheet + "\n" + source, e); 
     512        } 
     513        catch (FileNotFoundException e) 
     514        { 
     515            return transformError("XMLTransformer.transform(File, File)" + "\ncouldn't find the file specified\n" + stylesheet + "\n" + source, e); 
     516        } 
     517    } 
    472518    // debugAsFile is only to be set to true when either the stylesheet or source parameters  
    473519    // are not objects of type File. The debugAsFile variable is passed into the  
     
    479525        try 
    480526        { 
    481             TransformErrorListener transformerErrorListener = (TransformErrorListener) this.t_factory.getErrorListener(); 
     527                TransformErrorListener transformerErrorListener = (TransformErrorListener) this.t_factory.getErrorListener(); 
    482528            transformerErrorListener.setStylesheet(stylesheet); 
    483529            Transformer transformer = this.t_factory.newTransformer(new StreamSource(new InputStreamReader(new FileInputStream(stylesheet), "UTF-8")));