Changeset 16446


Ignore:
Timestamp:
2008-07-17T23:28:27+12:00 (16 years ago)
Author:
davidb
Message:

Replacement of Java 1.5 code with Java 1.4 equivalent, plus tidied up indentation

Location:
greenstone3/trunk/src/java/org/greenstone/gsdl3/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/core/Skin.java

    r16374 r16446  
    1919public class Skin {
    2020   
    21     public File rootDirectory ;
    22     public Document config ;
    23     private Receptionist receptionist ;
    24    
    25     protected DOMParser parser = null;
    26     TransformerFactory transformerFactory=null;
    27    
    28     // delete this later??
    29     //protected XMLConverter converter = null;
    30    
    31     private HashMap<String, String> xsltPagesByAction ;
    32    
    33     public Skin(Page page, Receptionist receptionist) throws Exception{
    34        
    35         this.receptionist = receptionist ;
    36         this.transformerFactory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
    37         //this.converter = new XMLConverter();
    38         transformerFactory.setURIResolver(new MyUriResolver()) ;
    39        
    40         this.parser = new DOMParser();
    41         this.parser.setFeature("http://xml.org/sax/features/validation", true);
    42         // don't try and load external DTD - no need if we are not validating, and may cause connection errors if a proxy is not set up.
    43         this.parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    44         // a performance test showed that having this on lead to increased
    45         // memory use for small-medium docs, and not much gain for large
    46         // docs.
    47         // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html
    48         this.parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
    49        
    50        
    51        
    52         String siteHome = GSFile.siteHome(GlobalProperties.getGSDL3Home(), page.getSite()) ;
    53         Document collectUi = page.getCollectUi() ;
    54        
    55         if (collectUi != null) {
    56             //System.out.println("looking for skin defined at collect level") ;
    57            
    58             String collectHome = GSFile.collectionBaseDir(siteHome, page.getCollection()) ;
    59             Element xmlSkin = (Element) GSXML.getChildByTagName(collectUi.getFirstChild(), "skin") ;
    60             if (xmlSkin != null) {
    61                 //System.out.println("skin defined at collect level") ;
     21    public File rootDirectory ;
     22    public Document config ;
     23    private Receptionist receptionist ;
     24   
     25    protected DOMParser parser = null;
     26    TransformerFactory transformerFactory=null;
     27   
     28    // delete this later??
     29    //protected XMLConverter converter = null;
     30   
     31    //private HashMap<String, String> xsltPagesByAction ;  // *****
     32    private HashMap xsltPagesByAction ;
     33   
     34    public Skin(Page page, Receptionist receptionist) throws Exception{
     35       
     36    this.receptionist = receptionist ;
     37    this.transformerFactory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
     38    //this.converter = new XMLConverter();
     39    transformerFactory.setURIResolver(new MyUriResolver()) ;
     40       
     41    this.parser = new DOMParser();
     42    this.parser.setFeature("http://xml.org/sax/features/validation", false);
     43    // don't try and load external DTD - no need if we are not validating, and may cause connection errors if a proxy is not set up.
     44    this.parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     45    // a performance test showed that having this on lead to increased
     46    // memory use for small-medium docs, and not much gain for large
     47    // docs.
     48    // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html
     49    this.parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
     50       
     51       
     52       
     53    String siteHome = GSFile.siteHome(GlobalProperties.getGSDL3Home(), page.getSite()) ;
     54    Document collectUi = page.getCollectUi() ;
     55       
     56    if (collectUi != null) {
     57        //System.out.println("looking for skin defined at collect level") ;
     58           
     59        String collectHome = GSFile.collectionBaseDir(siteHome, page.getCollection()) ;
     60        Element xmlSkin = (Element) GSXML.getChildByTagName(collectUi.getFirstChild(), "skin") ;
     61        if (xmlSkin != null) {
     62        //System.out.println("skin defined at collect level") ;
    6263               
    63                 boolean inherit = Boolean.parseBoolean(xmlSkin.getAttribute("inherit")) ;
     64        String inherit_att = xmlSkin.getAttribute("inherit") ;
     65        Boolean inherit_ref = Boolean.valueOf(inherit_att);
     66        boolean inherit = inherit_ref.booleanValue();
    6467               
    65                 if (inherit != true) {
    66                     boolean local = Boolean.parseBoolean(xmlSkin.getAttribute("local")) ;
    67                     if (local == true) {
    68                         rootDirectory = new File(collectHome + File.separatorChar + "ui") ;
    69                     } else {
    70                         String name = xmlSkin.getAttribute("name") ;
    71                         rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + File.separatorChar + name) ;
    72                     }
    73                 }               
    74             }
     68        if (inherit != true) {
     69           
     70            String local_att = xmlSkin.getAttribute("local") ;
     71            Boolean local_ref = Boolean.valueOf(local_att);
     72            boolean local = local_ref.booleanValue();
     73
     74            if (local == true) {
     75            rootDirectory = new File(collectHome + File.separatorChar + "ui") ;
     76            } else {
     77            String name = xmlSkin.getAttribute("name") ;
     78            rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + File.separatorChar + name) ;
     79            }
     80        }               
     81        }
     82    }
     83       
     84    Document siteUi = page.getSiteUi() ;
     85       
     86    if (rootDirectory == null && siteUi != null) {
     87        //System.out.println("lookding for skin defined at site level") ;
     88           
     89        Element xmlSkin = (Element) GSXML.getChildByTagName(siteUi.getFirstChild(), "skin") ;
     90        if (xmlSkin != null) {
     91        //System.out.println("skin defined at site level") ;
     92        String local_att = xmlSkin.getAttribute("local") ;
     93        Boolean local_ref = Boolean.valueOf(local_att);
     94        boolean local = local_ref.booleanValue();
     95
     96        if (local == true) {
     97            rootDirectory = new File(siteHome + File.separatorChar + "ui") ;
     98        } else {
     99            String name = xmlSkin.getAttribute("name") ;
     100            rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + name) ;
    75101        }
    76        
    77         Document siteUi = page.getSiteUi() ;
    78        
    79         if (rootDirectory == null && siteUi != null) {
    80             //System.out.println("lookding for skin defined at site level") ;
    81            
    82             Element xmlSkin = (Element) GSXML.getChildByTagName(siteUi.getFirstChild(), "skin") ;
    83             if (xmlSkin != null) {
    84                 //System.out.println("skin defined at site level") ;
    85                 boolean local = Boolean.parseBoolean(xmlSkin.getAttribute("local")) ;
    86                 if (local == true) {
    87                     rootDirectory = new File(siteHome + File.separatorChar + "ui") ;
    88                 } else {
    89                     String name = xmlSkin.getAttribute("name") ;
    90                     rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + name) ;
    91                 }
    92             }               
    93         }
    94        
    95         if(rootDirectory == null) {
    96             rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + "default") ;
    97         }
    98 
    99        
    100         File configFile = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "skin.xml") ;
    101         config = receptionist.converter.getDOM(configFile, "utf-8");
    102        
    103        
    104         // store which xslt pages are responsible for which actions
    105        
    106         xsltPagesByAction = new HashMap() ;
    107        
    108         NodeList actions = config.getElementsByTagName("action") ;
    109        
    110         for (int ai=0 , an=actions.getLength() ; ai<an ; ai++){
    111             Element action = (Element) actions.item(ai) ;
    112             String a = action.getAttribute("name") ;
    113            
    114             String xsl = action.getAttribute("xslt") ;
    115             if (!xsl.equals(""))
    116                 xsltPagesByAction.put(a, xsl) ;
    117            
    118             NodeList subactions = action.getElementsByTagName("subaction") ;
    119            
    120             for (int si=0 , sn=subactions.getLength() ; si<sn ; si++){
    121                 Element subaction = (Element) subactions.item(si) ;
     102        }               
     103    }
     104       
     105    if(rootDirectory == null) {
     106        rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + "default") ;
     107    }
     108
     109       
     110    File configFile = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "skin.xml") ;
     111    config = receptionist.converter.getDOM(configFile, "utf-8");
     112       
     113       
     114    // store which xslt pages are responsible for which actions
     115       
     116    xsltPagesByAction = new HashMap() ;
     117       
     118    NodeList actions = config.getElementsByTagName("action") ;
     119       
     120    for (int ai=0 , an=actions.getLength() ; ai<an ; ai++){
     121        Element action = (Element) actions.item(ai) ;
     122        String a = action.getAttribute("name") ;
     123           
     124        String xsl = action.getAttribute("xslt") ;
     125        if (!xsl.equals("")) {
     126        xsltPagesByAction.put(a, xsl) ;
     127        }
     128           
     129        NodeList subactions = action.getElementsByTagName("subaction") ;
     130           
     131        for (int si=0 , sn=subactions.getLength() ; si<sn ; si++){
     132        Element subaction = (Element) subactions.item(si) ;
    122133               
    123                 String sa = subaction.getAttribute("name") ;
    124                 xsl = subaction.getAttribute("xslt") ;
    125                 xsltPagesByAction.put(a + "_" + sa, xsl) ;
    126             }
    127         }
    128     }
    129    
    130     private Document getXSLTDoc(String action, String subaction) throws Exception {
    131         //System.out.println("getting xslt for " + action + ", " + subaction) ;
    132        
    133         String name = xsltPagesByAction.get(action + "_" + subaction) ;
    134        
    135         if (name == null)
    136             name = xsltPagesByAction.get(action) ;
    137        
    138         File xslt_file = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "xsl" + File.separatorChar + name) ;
    139         //System.out.println("Skinning page using: " + xslt_file) ;
    140        
    141         //if (!xslt_file.canRead())
    142         //  xslt_file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + "error.xsl") ;
    143        
    144         FileReader reader = new FileReader(xslt_file);
    145         InputSource xml_source = new InputSource(reader);
    146         this.parser.parse(xml_source);
    147         Document doc = this.parser.getDocument();
    148        
    149         return doc ;
    150     }
    151    
    152     private Document getPreprocessDoc() throws Exception {
     134        String sa = subaction.getAttribute("name") ;
     135        xsl = subaction.getAttribute("xslt") ;
     136        xsltPagesByAction.put(a + "_" + sa, xsl) ;
     137        }
     138    }
     139    }
     140   
     141    private Document getXSLTDoc(String action, String subaction) throws Exception {
     142    //System.out.println("getting xslt for " + action + ", " + subaction) ;
     143       
     144    String full_action = action + "_" + subaction;
     145    String name = (String)xsltPagesByAction.get(full_action) ;
     146       
     147    if (name == null) {
     148        name = (String)xsltPagesByAction.get(action);
     149    }
     150       
     151    File xslt_file = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "xsl" + File.separatorChar + name) ;
     152    //System.out.println("Skinning page using: " + xslt_file) ;
     153       
     154    //if (!xslt_file.canRead())
     155    //  xslt_file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + "error.xsl") ;
     156       
     157    FileReader reader = new FileReader(xslt_file);
     158    InputSource xml_source = new InputSource(reader);
     159    this.parser.parse(xml_source);
     160    Document doc = this.parser.getDocument();
     161       
     162    return doc ;
     163    }
     164   
     165    private Document getPreprocessDoc() throws Exception {
    153166                                                         
    154         File xslt_file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + "preProcess.xsl") ;
    155        
    156         FileReader reader = new FileReader(xslt_file);
    157         InputSource xml_source = new InputSource(reader);
    158         this.parser.parse(xml_source);
    159         Document doc = this.parser.getDocument();
     167    File xslt_file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + "preProcess.xsl") ;
     168       
     169    FileReader reader = new FileReader(xslt_file);
     170    InputSource xml_source = new InputSource(reader);
     171    this.parser.parse(xml_source);
     172    Document doc = this.parser.getDocument();
    160173       
    161         return doc ;       
    162     }
    163    
    164     private Document getLibraryDoc() throws Exception {
     174    return doc ;       
     175    }
     176   
     177    private Document getLibraryDoc() throws Exception {
    165178       
    166         File xslt_file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + "library.xsl") ;
    167        
    168         FileReader reader = new FileReader(xslt_file);
    169         InputSource xml_source = new InputSource(reader);
    170         this.parser.parse(xml_source);
    171         Document doc = this.parser.getDocument();
     179    File xslt_file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + "library.xsl") ;
     180       
     181    FileReader reader = new FileReader(xslt_file);
     182    InputSource xml_source = new InputSource(reader);
     183    this.parser.parse(xml_source);
     184    Document doc = this.parser.getDocument();
    172185       
    173         return doc ;       
    174     }
    175    
    176    
    177     public Node transformPage(Page page) throws Exception{
    178        
    179         Element p = page.getPage() ;
    180         Element pr = page.getPageResponse() ;
    181         Element transformedPage = null ;
    182        
    183         Element blah = receptionist.doc.createElement("Skin") ;
    184         blah.setAttribute("skinLocation", rootDirectory.getAbsolutePath()) ;
    185         pr.appendChild(blah) ;
    186        
    187         Document sourceXml ;
    188         try {
    189             sourceXml = receptionist.converter.newDOM();
    190             sourceXml.appendChild(sourceXml.importNode(p, true));
    191         } catch (Exception e) {
    192             System.out.println("error loading source data") ;
    193             e.printStackTrace() ;
    194             return constructErrorPage(e) ;
    195         }
    196        
    197         Document skinXsl ;
    198         try {
    199             skinXsl = getXSLTDoc(page.getAction(), page.getSubaction()) ;
    200         } catch (Exception e) {
    201             System.out.println("error loading skin xslt") ;
    202             e.printStackTrace() ;
    203             return constructErrorPage(e) ;
    204         }
    205        
    206         Document preprocessingXsl  ;
    207         try {
    208             preprocessingXsl = getPreprocessDoc() ;
    209         } catch (Exception e) {
    210             System.out.println("error loading preprocessing xslt") ;
    211         e.printStackTrace() ;
    212             return constructErrorPage(e) ;
    213         }
    214        
    215         Document libraryXsl ;
    216         try {
    217             libraryXsl = getLibraryDoc() ;
    218         } catch (Exception e) {
    219             System.out.println("error loading preprocessing xslt") ;
    220             e.printStackTrace() ;
    221             return constructErrorPage(e) ;
    222         }
    223        
    224         // combine skin file and library variables/templates into one document.
    225         //   we dont just use xsl:import because the preprocessing stage needs
    226         //   to know what's available in the library.
    227        
    228         Document skinAndLibraryXsl ;
    229         Document processedXsl = receptionist.converter.newDOM();
    230         try {
    231            
    232             skinAndLibraryXsl = receptionist.converter.newDOM();
    233             Element root = skinAndLibraryXsl.createElement("skinAndLibraryXsl") ;
    234             skinAndLibraryXsl.appendChild(root) ;
     186    return doc ;       
     187    }
     188   
     189   
     190    public Node transformPage(Page page) throws Exception {
     191       
     192       
     193    Element p = page.getPage() ;
     194    Element pr = page.getPageResponse() ;
     195    Element transformedPage = null ;
     196       
     197    Element blah = receptionist.doc.createElement("Skin") ;
     198    blah.setAttribute("skinLocation", rootDirectory.getAbsolutePath()) ;
     199    pr.appendChild(blah) ;
     200
     201    Document sourceXml ;
     202    try {
     203        sourceXml = receptionist.converter.newDOM();
     204        sourceXml.appendChild(sourceXml.importNode(p, true));
     205    } catch (Exception e) {
     206        System.out.println("error loading source data") ;
     207        e.printStackTrace() ;
     208        return constructErrorPage(e) ;
     209    }
     210
     211    Document skinXsl ;
     212    try {
     213        skinXsl = getXSLTDoc(page.getAction(), page.getSubaction()) ;
     214    } catch (Exception e) {
     215        System.out.println("error loading skin xslt") ;
     216        e.printStackTrace() ;
     217        return constructErrorPage(e) ;
     218    }
     219       
     220    Document preprocessingXsl  ;
     221    try {
     222        preprocessingXsl = getPreprocessDoc() ;
     223    } catch (Exception e) {
     224        System.out.println("error loading preprocessing xslt") ;
     225        e.printStackTrace() ;
     226        return constructErrorPage(e) ;
     227    }
     228       
     229    Document libraryXsl ;
     230    try {
     231        libraryXsl = getLibraryDoc() ;
     232    } catch (Exception e) {
     233        System.out.println("error loading preprocessing xslt") ;
     234        e.printStackTrace() ;
     235        return constructErrorPage(e) ;
     236    }
     237       
     238    // combine skin file and library variables/templates into one document.
     239    //   we dont just use xsl:import because the preprocessing stage needs
     240    //   to know what's available in the library.
     241       
     242    Document skinAndLibraryXsl ;
     243    Document processedXsl = receptionist.converter.newDOM();
     244    try {
     245           
     246        skinAndLibraryXsl = receptionist.converter.newDOM();
     247        Element root = skinAndLibraryXsl.createElement("skinAndLibraryXsl") ;
     248        skinAndLibraryXsl.appendChild(root) ;
     249
     250        Element s = skinAndLibraryXsl.createElement("skinXsl") ;
     251        s.appendChild(skinAndLibraryXsl.importNode(skinXsl.getDocumentElement(), true)) ;
     252        root.appendChild(s) ;
     253
     254        Element l = skinAndLibraryXsl.createElement("libraryXsl") ;
     255        l.appendChild(skinAndLibraryXsl.importNode(libraryXsl.getDocumentElement(), true)) ;
     256        root.appendChild(l) ;
     257           
     258           
     259        // System.out.println("Pre - processing") ;
     260        //pre-process the skin style sheet
     261        Transformer preProcessor = transformerFactory.newTransformer(new DOMSource(preprocessingXsl));
     262        DOMResult result = new DOMResult();
     263        result.setNode(processedXsl);
     264        preProcessor.transform(new DOMSource(skinAndLibraryXsl), result);
     265    } catch (TransformerException e) {
     266        e.printStackTrace() ;
     267        return constructErrorPage(e) ;
     268    }  catch (Exception e) {
     269        System.out.println("error preprocessing the skin xslt") ;
     270        e.printStackTrace() ;
     271        return constructErrorPage(e) ;
     272    }
     273       
     274    //return sourceXml.getDocumentElement() ;
     275       
     276    // Document finalPage = receptionist.converter.newDOM(); // ****
     277    Document finalPageParsed = null;
     278
     279    //transform source doc
     280    try {
     281       
     282        /*
     283        //Tranform XSL DOM to String   
     284        TransformerFactory tf = TransformerFactory.newInstance();
     285        Transformer trans = tf.newTransformer();
     286        StringWriter sw = new StringWriter();
     287        trans.transform(new DOMSource(processedXsl), new StreamResult(sw));
     288        // String theXSLAnswer = sw.toString(); // ****
     289           
     290           
     291        //Tranform XML DOM to String   
     292        TransformerFactory tf2 = TransformerFactory.newInstance();
     293        Transformer trans2 = tf2.newTransformer();
     294        StringWriter sw2 = new StringWriter();
     295        trans2.transform(new DOMSource(sourceXml), new StreamResult(sw2));
     296        //String theXMLAnswer = sw2.toString();
     297        */
     298
     299        Transformer transformer = transformerFactory.newTransformer(new DOMSource(processedXsl));
     300        // transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
     301        //DOMResult result = new DOMResult();
     302        //result.setNode(finalPage);
     303           
     304        //transformer.transform(new DOMSource(sourceXml), result2);
     305        StringWriter result2 = new StringWriter();
     306        transformer.transform(new DOMSource(sourceXml), new StreamResult(result2));
     307
     308        XMLConverter converter = new XMLConverter();
     309
     310        finalPageParsed = converter.getDOM(result2.toString());                     
     311           
     312    } catch (TransformerException e) {
     313        e.printStackTrace() ;
     314        return constructErrorPage(e) ;
     315    } catch (Exception e) {
     316       
     317        System.out.println("error transforming page") ;
     318        e.printStackTrace() ;
     319           
     320        //return processedXsl.getDocumentElement() ;
     321        return constructErrorPage(e) ;
     322    }
     323           
     324    return finalPageParsed;
     325    }
     326   
     327    private Element constructErrorPage(TransformerException exception) {
     328    Document doc = receptionist.doc;
     329    Element page = doc.createElement("page") ;
     330       
     331    Element header = doc.createElement("h1") ;
     332
     333
     334    // header.setTextContent("Error");  // Java 1.5
     335    header.appendChild(doc.createTextNode("Error")); // Java 1.4
     336
     337    page.appendChild(header) ;
     338       
     339    Element prompt = doc.createElement("p") ;
     340    // prompt.setTextContent("The following exception occured: ") ;
     341    prompt.appendChild(doc.createTextNode("The following exception occured: "));
     342    page.appendChild((prompt)) ;
     343       
     344    Element errorXml = doc.createElement("code") ;
     345    //errorXml.setTextContent(exception.getMessageAndLocation()) ;
     346    errorXml.appendChild(doc.createTextNode(exception.getMessageAndLocation())) ;
     347    page.appendChild(errorXml) ;
     348       
     349    return page ;
     350       
     351    }
     352   
     353    private Element constructErrorPage(Exception exception) {
     354    Document doc = receptionist.doc;
     355    Element page = doc.createElement("page") ;
     356       
     357    Element header = doc.createElement("h1") ;
     358    //header.setTextContent("Error"); // Java 1.5
     359    header.appendChild(doc.createTextNode("Error")); // Java 1.4
     360
     361    page.appendChild(header) ;
     362       
     363    Element prompt = doc.createElement("p") ;
     364    // prompt.setTextContent("The following exception occured: ") ;
     365    prompt.appendChild(doc.createTextNode("The following exception occured: "));
     366
     367    page.appendChild((prompt)) ;
     368       
     369    Element errorXml = doc.createElement("code") ;
     370    //errorXml.setTextContent(exception.toString()) ;
     371    errorXml.appendChild(doc.createTextNode(exception.toString())) ;
     372    page.appendChild(errorXml) ;
     373       
     374    return page ;
     375    }
     376   
     377
     378    private Element constructErrorPage(Element source, Document style, Exception exception)
     379    {
     380    Document doc = receptionist.doc;
     381    Element page = doc.createElement("page") ;
     382       
     383    Element header = doc.createElement("h1") ;
     384    //header.setTextContent("Error") ; // Java 1.5
     385    header.appendChild(doc.createTextNode("Error")); // Java 1.4
     386
     387    page.appendChild(header) ;
     388       
     389    Element prompt = doc.createElement("p") ;
     390    //prompt.setTextContent("The following exception occured: ") ;
     391    prompt.appendChild(doc.createTextNode("The following exception occured: "));
     392        page.appendChild((prompt)) ;
     393       
     394    Element errorXml = doc.createElement("code") ;
     395    //errorXml.setTextContent(exception.getMessage()) ;
     396    errorXml.appendChild(doc.createTextNode(exception.getMessage())) ;
     397    page.appendChild(errorXml) ;
     398       
     399    /*
     400      Element stackTrace = doc.createElement("ul") ;
     401       
     402      StackTraceElement[] st = exception.getStackTrace() ;
     403      for (int i=0 ; i< st.length ; i++) {
     404      Element ste = doc.createElement("li") ;
     405      ste.setTextContent(st[i].toString()) ;
     406      stackTrace.appendChild(ste) ;
     407      }
     408      errorMessage.appendChild(stackTrace) ;
     409    */
     410       
     411        Element sourceHeader = doc.createElement("h2") ;
     412    //sourceHeader.setTextContent("Source page:") ;
     413    sourceHeader.appendChild(doc.createTextNode("Source page:")) ;
     414    page.appendChild(sourceHeader) ;
     415       
     416        Element sourceXml = doc.createElement("pre") ;
     417    String source_pp = receptionist.converter.getPrettyString(source);
     418    //sourceXml.setTextContent(source_pp) ;
     419    sourceXml.appendChild(doc.createTextNode(source_pp)) ;
     420    page.appendChild(sourceXml) ;
     421       
     422        Element styleHeader = doc.createElement("h2") ;
     423    //styleHeader.setTextContent("Style page:") ;
     424    styleHeader.appendChild(doc.createTextNode("Style page:")) ;
     425        page.appendChild(styleHeader) ;
     426       
     427        Element styleXml = doc.createElement("pre") ;
     428    String style_pp = receptionist.converter.getPrettyString(style);
     429
     430    //styleXml.setTextContent(style_pp);
     431    styleXml.appendChild(doc.createTextNode(style_pp));
     432    page.appendChild(styleXml) ;
     433       
     434       
     435    return (Element)page ;
     436    }
     437   
     438    private class MyUriResolver implements URIResolver {
     439       
     440    public Source resolve(String href, String base) {
     441           
     442        System.out.println("resolving href='" + href + "', base='" + base + "'") ;
    235443                       
    236             Element s = skinAndLibraryXsl.createElement("skinXsl") ;
    237             s.appendChild(skinAndLibraryXsl.importNode(skinXsl.getDocumentElement(), true)) ;
    238             root.appendChild(s) ;
    239            
    240             Element l = skinAndLibraryXsl.createElement("libraryXsl") ;
    241             l.appendChild(skinAndLibraryXsl.importNode(libraryXsl.getDocumentElement(), true)) ;
    242             root.appendChild(l) ;
    243            
    244            
    245             System.out.println("Pre - processing") ;
    246             //pre-process the skin style sheet
    247             Transformer preProcessor = transformerFactory.newTransformer(new DOMSource(preprocessingXsl));
    248             DOMResult result = new DOMResult();
    249             result.setNode(processedXsl) ;
    250             preProcessor.transform(new DOMSource(skinAndLibraryXsl), result);
    251         } catch (TransformerException e) {
    252             return constructErrorPage(e) ;
    253         }  catch (Exception e) {
    254             System.out.println("error preprocessing the skin xslt") ;
    255             e.printStackTrace() ;
    256             return constructErrorPage(e) ;
    257         }
    258        
    259         //return sourceXml.getDocumentElement() ;
    260        
    261         // Document finalPage = receptionist.converter.newDOM(); // ****
    262         Document finalPageParsed = null;
    263 
    264         //transform source doc
    265         try {
    266        
    267             /*
    268             //Tranform XSL DOM to String   
    269             TransformerFactory tf = TransformerFactory.newInstance();
    270             Transformer trans = tf.newTransformer();
    271             StringWriter sw = new StringWriter();
    272             trans.transform(new DOMSource(processedXsl), new StreamResult(sw));
    273             // String theXSLAnswer = sw.toString(); // ****
    274            
    275            
    276             //Tranform XML DOM to String   
    277             TransformerFactory tf2 = TransformerFactory.newInstance();
    278             Transformer trans2 = tf2.newTransformer();
    279             StringWriter sw2 = new StringWriter();
    280             trans2.transform(new DOMSource(sourceXml), new StreamResult(sw2));
    281             //String theXMLAnswer = sw2.toString();
    282             */
    283 
    284             Transformer transformer = transformerFactory.newTransformer(new DOMSource(processedXsl));
    285             // transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
    286             //DOMResult result = new DOMResult();
    287             //result.setNode(finalPage);
    288            
    289             //transformer.transform(new DOMSource(sourceXml), result2);
    290             StringWriter result2 = new StringWriter();
    291             transformer.transform(new DOMSource(sourceXml), new StreamResult(result2));
    292 
    293             XMLConverter converter = new XMLConverter();
    294             finalPageParsed = converter.getDOM(result2.toString());                     
    295            
    296         } catch (TransformerException e) {
    297             return constructErrorPage(e) ;
    298         } catch (Exception e) {
    299        
    300             System.out.println("error transforming page") ;
    301             e.printStackTrace() ;
    302            
    303             //return processedXsl.getDocumentElement() ;
    304             return constructErrorPage(e) ;
    305         }
    306            
    307         return finalPageParsed;
    308     }
    309    
    310     private Element constructErrorPage(TransformerException exception) {
    311         Element page = receptionist.doc.createElement("page") ;
    312        
    313         Element header = receptionist.doc.createElement("h1") ;
    314         header.setTextContent("Error") ;
    315         page.appendChild(header) ;
    316        
    317         Element prompt = receptionist.doc.createElement("p") ;
    318         prompt.setTextContent("The following exception occured: ") ;
    319         page.appendChild((prompt)) ;
    320        
    321         Element errorXml = receptionist.doc.createElement("code") ;
    322         errorXml.setTextContent(exception.getMessageAndLocation()) ;
    323         page.appendChild(errorXml) ;
    324        
    325         return page ;
    326        
    327     }
    328    
    329     private Element constructErrorPage(Exception exception) {
    330         Element page = receptionist.doc.createElement("page") ;
    331        
    332         Element header = receptionist.doc.createElement("h1") ;
    333         header.setTextContent("Error") ;
    334         page.appendChild(header) ;
    335        
    336         Element prompt = receptionist.doc.createElement("p") ;
    337         prompt.setTextContent("The following exception occured: ") ;
    338         page.appendChild((prompt)) ;
    339        
    340         Element errorXml = receptionist.doc.createElement("code") ;
    341         errorXml.setTextContent(exception.toString()) ;
    342         page.appendChild(errorXml) ;
    343        
    344        
    345         return page ;
    346     }
    347    
    348 
    349     private Element constructErrorPage(Element source, Document style, Exception exception) {
    350         Element page = receptionist.doc.createElement("page") ;
    351        
    352         Element header = receptionist.doc.createElement("h1") ;
    353         header.setTextContent("Error") ;
    354         page.appendChild(header) ;
    355        
    356         Element prompt = receptionist.doc.createElement("p") ;
    357         prompt.setTextContent("The following exception occured: ") ;
    358         page.appendChild((prompt)) ;
    359        
    360         Element errorXml = receptionist.doc.createElement("code") ;
    361         errorXml.setTextContent(exception.getMessage()) ;
    362         page.appendChild(errorXml) ;
    363        
    364         /*
    365         Element stackTrace = receptionist.doc.createElement("ul") ;
    366        
    367         StackTraceElement[] st = exception.getStackTrace() ;
    368         for (int i=0 ; i< st.length ; i++) {
    369             Element ste = receptionist.doc.createElement("li") ;
    370             ste.setTextContent(st[i].toString()) ;
    371             stackTrace.appendChild(ste) ;
    372         }
    373         errorMessage.appendChild(stackTrace) ;
    374         */
    375        
    376         Element sourceHeader = receptionist.doc.createElement("h2") ;
    377         sourceHeader.setTextContent("Source page:") ;
    378         page.appendChild(sourceHeader) ;
    379        
    380         Element sourceXml = receptionist.doc.createElement("pre") ;
    381         sourceXml.setTextContent(receptionist.converter.getPrettyString(source)) ;
    382         page.appendChild(sourceXml) ;
    383        
    384         Element styleHeader = receptionist.doc.createElement("h2") ;
    385         styleHeader.setTextContent("Style page:") ;
    386         page.appendChild(styleHeader) ;
    387        
    388         Element styleXml = receptionist.doc.createElement("pre") ;
    389         styleXml.setTextContent(receptionist.converter.getPrettyString(style)) ;
    390         page.appendChild(styleXml) ;
    391        
    392        
    393         return (Element)page ;
    394     }
    395    
    396     private class MyUriResolver implements URIResolver {
    397        
    398         public Source resolve(String href, String base) {
    399            
    400             System.out.println("resolving href='" + href + "', base='" + base + "'") ;
    401                        
    402             // check in the skin directory first
    403            
    404             File file = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "xslt" + File.separatorChar + href) ;
    405            
    406             // then check in the xslt library directory
    407             if (!file.canRead())
    408                 file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + href) ;
    409            
    410             if (file.canRead()) {
    411                 Source source = new StreamSource(file) ;
    412                 return source ;
    413             } else
    414                 return null ;
    415         }
    416        
    417     }
     444        // check in the skin directory first
     445           
     446        File file = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "xslt" + File.separatorChar + href) ;
     447           
     448        // then check in the xslt library directory
     449        if (!file.canRead())
     450        file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + href) ;
     451           
     452        if (file.canRead()) {
     453        Source source = new StreamSource(file) ;
     454        return source ;
     455        } else
     456        return null ;
     457    }
     458       
     459    }
    418460   
    419461
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/core/SkinnedReceptionist.java

    r16374 r16446  
    11package org.greenstone.gsdl3.core;
    22
    3 import java.util.* ;
    4 import java.io.* ;
     3import java.util.*;
     4import java.io.*;
    55
    66import org.w3c.dom.NodeList;
     
    1111import org.greenstone.gsdl3.util.*;
    1212
    13 import javax.xml.xpath.* ;
     13import javax.xml.xpath.*;
    1414
    1515public class SkinnedReceptionist extends NZDLReceptionist {
    1616   
    17     /** a transformer class to transform xml using xslt */
    18     protected XMLTransformer transformer=null; 
    19    
    20     public SkinnedReceptionist() {
    21         super();
    22         System.out.println("Skinned Receptionist created") ;
    23         //this.xslt_map = new HashMap();
    24         this.transformer = new XMLTransformer();
    25     }
    26    
    27     protected Node postProcessPage(Element p) {
    28         try {
    29             //Avoid the duplicate attribute in the namespaces declaration of the generated html file
    30             //String NAMESPACE_URI = "http://www.greenstone.org/greenstone3/schema/gsf";
    31             //String NAMESPACE_PREFIX = "gsf";
    32             //p.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + NAMESPACE_PREFIX, NAMESPACE_URI);
    33            
    34            
    35             Page page = new Page(p, this) ;
    36             Skin skin = new Skin(page, this) ;
    37            
    38             addExtraInfo(p) ;
    39             addCssFiles(page, skin) ;
    40             addJsFiles(page, skin) ;
    41             addUiElements(page) ;
    42             addMetadataElements(page) ;
    43            
    44             String output = page.getPageRequest().getAttribute(GSXML.OUTPUT_ATT);
    45            
    46             // might need to add some data to the page
    47             //addExtraInfo(page);
    48            
    49            
    50             System.out.println("action = " + page.getAction()) ;
    51             //  if we are displaying a document, specify an attribute for weither a cover image exists.
    52             if (page.getAction().equals("d")) {
    53                
    54                 System.out.println("doing the document thing") ;
    55                
    56                 Element document = (Element)p.getElementsByTagName("document").item(0) ;
    57                 Element metadataList = (Element)GSXML.getChildByTagName(document, "metadataList")  ;
    58                 String archiveDir = "" ;
    59                 NodeList metadata = metadataList.getChildNodes() ;
    60                 for (int i=0 ; i<metadata.getLength() ; i++) {
    61                     Element m = (Element) metadata.item(i) ;
    62                     if (m.getAttribute("name").equals("archivedir"))
    63                         archiveDir = GSXML.getNodeText(m) ;
    64                 }
    65                
    66                 String coverImage_path = page.getCollectionHome()
    67                     + File.separatorChar + "index" 
    68                     + File.separatorChar + "assoc" 
    69                     + File.separatorChar + archiveDir 
    70                     + File.separatorChar + "cover.jpg" ;       
    71                
    72                 document.setAttribute("coverImage", coverImage_path) ;
    73                
    74                 File coverImage = new File(coverImage_path) ;
    75                 document.setAttribute("hasCoverImage", String.valueOf(coverImage.canRead())) ;
    76             }
    77            
    78            
    79             Node np = null;
    80             try {
    81                 if (skin != null && !output.equals("xml")) {
    82                     np = skin.transformPage(page);
    83                 }
    84                 else {
    85                     np = p;
    86                 }
    87             } catch (Exception e) {
    88                 e.printStackTrace() ;
    89             }
    90            
    91            
    92            
    93             return np ;
    94         } catch (Exception e) {
    95             e.printStackTrace() ;
    96             return doc.createElement("page") ;
    97         }
    98     }
    99    
    100     /** overwrite this to add any extra info that might be needed in the page before transformation */
    101     protected void addExtraInfo(Element page) {
    102 
    103         //logger.debug("the  page before adding extra info is");
    104         //logger.debug(this.converter.getPrettyString(page));
    105        
    106         super.addExtraInfo(page);
    107        
    108         // this gets the collection info
    109         Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
    110         Element page_request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
    111         Element collection_info = (Element)GSXML.getChildByTagName(page_response, GSXML.COLLECTION_ELEM);
    112         if (collection_info== null) {
    113             return;
    114         }
    115        
    116         String collection = collection_info.getAttribute("name"); // should we take this from the request instead??
    117         // look through services and see if classifier one is present - need to
    118         // get the list of classifiers
    119         Element service_list = (Element)GSXML.getChildByTagName(collection_info, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
    120         if (service_list == null) {
    121             // something weird has gone wrong
    122             return;
    123         }
    124        
    125         Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
    126         String lang = page.getAttribute(GSXML.LANG_ATT);
    127        
    128         boolean do_classifier = false;
    129         boolean do_query = false;
    130         Element classifier_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "ClassifierBrowse");
    131         Element query_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "TextQuery");
    132         if (classifier_service != null) {
    133             do_classifier = true;
    134             // find the list of classifiers
    135            
    136             String to = GSPath.appendLink(collection, "ClassifierBrowse");
    137             Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, "");
    138             info_message.appendChild(info_request);
    139         }
    140        
    141         String action = page_request.getAttribute(GSXML.ACTION_ATT);
    142         String subaction = page_request.getAttribute(GSXML.SUBACTION_ATT);
    143        
    144         if (query_service != null && action.equals("p") && subaction.equals("about")) {
    145             do_query = true;
    146             // get the full description
    147             String to = GSPath.appendLink(collection, "TextQuery");
    148             Element query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, "");
    149             info_message.appendChild(query_request);
    150            
    151         }
    152        
    153         Element info_resp_message = (Element) this.mr.process(info_message);
    154         Element info_response = (Element) GSXML.getChildByTagName(info_resp_message, GSXML.RESPONSE_ELEM);
    155         //  String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM);
    156         if (do_classifier) {
    157             Element classifier_service_info = (Element)GSXML.getNamedElement(info_response, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "CLassifierBrowse");
    158             if (classifier_service_info != null) {
    159                 service_list.replaceChild(this.doc.importNode(classifier_service_info, true), classifier_service);
    160             }
    161         }
    162         if (do_query) {
    163             Element query_service_info = (Element)GSXML.getNamedElement(info_response, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "TextQuery");
    164             if (query_service_info != null) {
    165                 service_list.replaceChild(this.doc.importNode(query_service_info, true), query_service);
    166             }
    167         }
    168         //logger.debug("the final page before transforming is");
    169         //logger.debug(this.converter.getPrettyString(page));
    170         return;
    171     }
    172    
    173     protected void addCssFiles(Page page, Skin skin) {
    174        
    175         Element cssFileList = doc.createElement("cssFileList") ;
    176        
    177         // get skin level files
    178         if (skin != null) {
    179             File dir = new File(skin.rootDirectory.getPath() + File.separatorChar + "css") ;
    180             if (dir.isDirectory()) {
    181                 File[] files = dir.listFiles() ;
    182                
    183                 for (int fi=0, fn=files.length ; fi<fn ; fi++) {
    184                     if (files[fi].getName().endsWith(".css")) {
    185                         Element cssFile = doc.createElement("cssFile") ;
    186                         String path = files[fi].getPath() ;
    187                         path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
    188                             //changes to a correct path in case GS3 runs on Windows
    189                             path = path.replace( '\\', '/' );   
    190                         cssFile.setAttribute("path", path) ;
    191                         cssFileList.appendChild(cssFile) ;
    192                     }
    193                 }
    194             }           
     17    /** a transformer class to transform xml using xslt */
     18    protected XMLTransformer transformer=null; 
     19   
     20    public SkinnedReceptionist() {
     21    super();
     22    System.out.println("Skinned Receptionist created");
     23    //this.xslt_map = new HashMap();
     24    this.transformer = new XMLTransformer();
     25    }
     26   
     27    protected Node postProcessPage(Element p) {
     28    try {
     29        //Avoid the duplicate attribute in the namespaces declaration of the generated html file
     30        //String NAMESPACE_URI = "http://www.greenstone.org/greenstone3/schema/gsf";
     31        //String NAMESPACE_PREFIX = "gsf";
     32        //p.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + NAMESPACE_PREFIX, NAMESPACE_URI);
     33           
     34           
     35        Page page = new Page(p, this);
     36        Skin skin = new Skin(page, this);
     37           
     38        addExtraInfo(p);
     39        addCssFiles(page, skin);
     40        addJsFiles(page, skin);
     41        addUiElements(page);
     42        addMetadataElements(page);
     43           
     44        String output = page.getPageRequest().getAttribute(GSXML.OUTPUT_ATT);
     45           
     46        // might need to add some data to the page
     47        //addExtraInfo(page);
     48           
     49           
     50        System.out.println("action = " + page.getAction());
     51        //  if we are displaying a document, specify an attribute for weither a cover image exists.
     52        if (page.getAction().equals("d")) {
     53               
     54        System.out.println("doing the document thing");
     55               
     56        Element document = (Element)p.getElementsByTagName("document").item(0);
     57        Element metadataList = (Element)GSXML.getChildByTagName(document, "metadataList");
     58        String archiveDir = "";
     59        NodeList metadata = metadataList.getChildNodes();
     60        for (int i=0; i<metadata.getLength(); i++) {
     61            Element m = (Element) metadata.item(i);
     62            if (m.getAttribute("name").equals("archivedir"))
     63            archiveDir = GSXML.getNodeText(m);
     64        }
     65               
     66        String coverImage_path = page.getCollectionHome()
     67            + File.separatorChar + "index" 
     68            + File.separatorChar + "assoc" 
     69            + File.separatorChar + archiveDir 
     70            + File.separatorChar + "cover.jpg";     
     71               
     72        document.setAttribute("coverImage", coverImage_path);
     73               
     74        File coverImage = new File(coverImage_path);
     75        document.setAttribute("hasCoverImage", String.valueOf(coverImage.canRead()));
     76        }
     77           
     78           
     79        Node np = null;
     80        try {
     81
     82        if (skin != null && !output.equals("xml")) {
     83            System.err.println("Away to to skin transformPage");
     84            np = skin.transformPage(page);
     85            System.err.println("done");
     86
     87                   
     88        }
     89        else {
     90            np = p;
     91        }
     92        } catch (Exception e) {
     93        e.printStackTrace();
     94        }
     95           
     96           
     97           
     98        return np;
     99    } catch (Exception e) {
     100        e.printStackTrace();
     101        return doc.createElement("page");
     102    }
     103    }
     104   
     105    /** overwrite this to add any extra info that might be needed in the page before transformation */
     106    protected void addExtraInfo(Element page) {
     107
     108    //logger.debug("the  page before adding extra info is");
     109    //logger.debug(this.converter.getPrettyString(page));
     110       
     111    super.addExtraInfo(page);
     112       
     113    // this gets the collection info
     114    Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
     115    Element page_request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
     116    Element collection_info = (Element)GSXML.getChildByTagName(page_response, GSXML.COLLECTION_ELEM);
     117    if (collection_info== null) {
     118        return;
     119    }
     120       
     121    String collection = collection_info.getAttribute("name"); // should we take this from the request instead??
     122    // look through services and see if classifier one is present - need to
     123    // get the list of classifiers
     124    Element service_list = (Element)GSXML.getChildByTagName(collection_info, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
     125    if (service_list == null) {
     126        // something weird has gone wrong
     127        return;
     128    }
     129       
     130    Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
     131    String lang = page.getAttribute(GSXML.LANG_ATT);
     132       
     133    boolean do_classifier = false;
     134    boolean do_query = false;
     135    Element classifier_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "ClassifierBrowse");
     136    Element query_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "TextQuery");
     137    if (classifier_service != null) {
     138        do_classifier = true;
     139        // find the list of classifiers
     140           
     141        String to = GSPath.appendLink(collection, "ClassifierBrowse");
     142        Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, "");
     143        info_message.appendChild(info_request);
     144    }
     145       
     146    String action = page_request.getAttribute(GSXML.ACTION_ATT);
     147    String subaction = page_request.getAttribute(GSXML.SUBACTION_ATT);
     148       
     149    if (query_service != null && action.equals("p") && subaction.equals("about")) {
     150        do_query = true;
     151        // get the full description
     152        String to = GSPath.appendLink(collection, "TextQuery");
     153        Element query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, "");
     154        info_message.appendChild(query_request);
     155           
     156    }
     157       
     158    Element info_resp_message = (Element) this.mr.process(info_message);
     159    Element info_response = (Element) GSXML.getChildByTagName(info_resp_message, GSXML.RESPONSE_ELEM);
     160    //  String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM);
     161    if (do_classifier) {
     162        Element classifier_service_info = (Element)GSXML.getNamedElement(info_response, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "CLassifierBrowse");
     163        if (classifier_service_info != null) {
     164        service_list.replaceChild(this.doc.importNode(classifier_service_info, true), classifier_service);
     165        }
     166    }
     167    if (do_query) {
     168        Element query_service_info = (Element)GSXML.getNamedElement(info_response, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "TextQuery");
     169        if (query_service_info != null) {
     170        service_list.replaceChild(this.doc.importNode(query_service_info, true), query_service);
     171        }
     172    }
     173    //logger.debug("the final page before transforming is");
     174    //logger.debug(this.converter.getPrettyString(page));
     175    return;
     176    }
     177   
     178    protected void addCssFiles(Page page, Skin skin) {
     179       
     180    Element cssFileList = doc.createElement("cssFileList");
     181       
     182    // get skin level files
     183    if (skin != null) {
     184        File dir = new File(skin.rootDirectory.getPath() + File.separatorChar + "css");
     185        if (dir.isDirectory()) {
     186        File[] files = dir.listFiles();
     187               
     188        for (int fi=0, fn=files.length; fi<fn; fi++) {
     189            if (files[fi].getName().endsWith(".css")) {
     190            Element cssFile = doc.createElement("cssFile");
     191            String path = files[fi].getPath();
     192            path = "." + path.substring(GlobalProperties.getGSDL3Home().length());
     193            //changes to a correct path in case GS3 runs on Windows
     194            path = path.replace( '\\', '/' );   
     195            cssFile.setAttribute("path", path);
     196            cssFileList.appendChild(cssFile);
     197            }
     198        }
     199        }           
     200    }   
     201       
     202    // get site level files
     203    String site_home = page.getSiteHome();
     204    File dir = new File(site_home + File.separatorChar + "ui" + File.separatorChar + "css");
     205    if (dir.isDirectory()) {
     206        File[] files = dir.listFiles();
     207        for (int fi=0, fn=files.length; fi<fn; fi++) {
     208        if (files[fi].getName().endsWith(".css")) {
     209            Element cssFile = doc.createElement("cssFile");
     210            String path = files[fi].getPath();
     211            path = "." + path.substring(GlobalProperties.getGSDL3Home().length());
     212            //changes to a correct path in case GS3 runs on Windows
     213            path = path.replace( '\\', '/' );                   
     214            cssFile.setAttribute("path", path);
     215            cssFileList.appendChild(cssFile);
     216        }
     217        }
     218    }
     219       
     220    // get collect level files
     221    String collect_home = page.getCollectionHome();
     222    if (collect_home != "") {
     223        dir = new File(collect_home + File.separatorChar + "ui" + File.separatorChar + "css");
     224        if (dir.isDirectory()) {
     225        File[] files = dir.listFiles();
     226        for (int fi=0, fn=files.length; fi<fn; fi++) {
     227            if (files[fi].getName().endsWith(".css")) {
     228            Element cssFile = doc.createElement("cssFile");
     229            String path = files[fi].getPath();
     230            path = "." + path.substring(GlobalProperties.getGSDL3Home().length());
     231            //changes to a correct path in case GS3 runs on Windows
     232            path = path.replace( '\\', '/' );       
     233            cssFile.setAttribute("path", path);
     234            cssFileList.appendChild(cssFile);
     235            }
     236        }
     237        }
     238    }
     239       
     240    page.getPageResponse().appendChild(cssFileList);
     241    }
     242   
     243    protected void addJsFiles(Page page, Skin skin) {
     244       
     245    Element jsFileList = doc.createElement("jsFileList");
     246       
     247    // get skin level files
     248    if (skin != null) {
     249        File dir = new File(skin.rootDirectory.getPath() + File.separatorChar + "js");
     250        if (dir.isDirectory()) {
     251        File[] files = dir.listFiles();
     252               
     253        for (int fi=0, fn=files.length; fi<fn; fi++) {
     254            if (files[fi].getName().endsWith(".js")) {
     255            Element jsFile = doc.createElement("jsFile");
     256            String path = files[fi].getPath();
     257            path = "." + path.substring(GlobalProperties.getGSDL3Home().length());
     258            //changes to a correct path in case GS3 runs on Windows
     259            path = path.replace( '\\', '/' );   
     260            jsFile.setAttribute("path", path);
     261            jsFileList.appendChild(jsFile);
     262            }
     263        }
     264        }           
     265    }   
     266       
     267    // get site level files
     268    String site_home = page.getSiteHome();
     269    File dir = new File(site_home + File.separatorChar + "ui" + File.separatorChar + "js");
     270    if (dir.isDirectory()) {
     271        File[] files = dir.listFiles();
     272        for (int fi=0, fn=files.length; fi<fn; fi++) {
     273        if (files[fi].getName().endsWith(".js")) {
     274            Element jsFile = doc.createElement("jsFile");
     275            String path = files[fi].getPath();
     276            path = "." + path.substring(GlobalProperties.getGSDL3Home().length());
     277            //changes to a correct path in case GS3 runs on Windows
     278            path = path.replace( '\\', '/' );   
     279            jsFile.setAttribute("path", path);
     280            jsFileList.appendChild(jsFile);
     281        }
     282        }
     283    }
     284       
     285    // get collect level files
     286    String collect_home = page.getCollectionHome();
     287    if (collect_home != "") {
     288        dir = new File(collect_home + File.separatorChar + "ui" + File.separatorChar + "js");
     289        if (dir.isDirectory()) {
     290        File[] files = dir.listFiles();
     291        for (int fi=0, fn=files.length; fi<fn; fi++) {
     292            if (files[fi].getName().endsWith(".js")) {
     293            Element jsFile = doc.createElement("jsFile");
     294            String path = files[fi].getPath();
     295            path = "." + path.substring(GlobalProperties.getGSDL3Home().length());
     296            //changes to a correct path in case GS3 runs on Windows
     297            path = path.replace( '\\', '/' );   
     298            jsFile.setAttribute("path", path);
     299            jsFileList.appendChild(jsFile);
     300            }
     301        }
     302        }
     303    }
     304       
     305    page.getPageResponse().appendChild(jsFileList);
     306    }
     307   
     308    protected void addUiElements(Page page) {
     309       
     310    Document siteUi = page.getSiteUi();
     311    Document collectUi = page.getCollectUi();
     312    String lang = page.getLanguage();
     313       
     314    HashMap elements = getRelevantElements("uiItem", lang, siteUi, collectUi);
     315    Element xmlElements = doc.createElement("uiItemList");
     316       
     317    Iterator i = elements.values().iterator();
     318    while (i.hasNext()) {
     319        Element element = (Element)i.next();
     320           
     321        // doc.adoptNode(element); // Java 1.5 (DOM Level 3)
     322        // xmlElements.appendChild(element);
     323
     324        // Java 1.4 equivalent
     325        Node doc_owned_node = doc.importNode(element,true);
     326        // Should also remove element from its original parent, as adopt does?
     327        // element.getParentNode().removeChild(element);
     328
     329
     330        xmlElements.appendChild(doc_owned_node);
     331           
     332           
     333    }
     334       
     335    page.getPageResponse().appendChild(xmlElements);
     336    }
     337   
     338    protected void addMetadataElements(Page page) {
     339    Document siteMetadata = page.getSiteMetadata();
     340    Document collectMetadata = page.getCollectMetadata();
     341    String lang = page.getLanguage();
     342       
     343    HashMap elements = getRelevantElements("metadataItem", lang, siteMetadata, collectMetadata);
     344    Element xmlElements = doc.createElement("metadataList");
     345       
     346    Iterator i = elements.values().iterator();
     347    while (i.hasNext()) {
     348        Element element = (Element)i.next();
     349       
     350        // doc.adoptNode(element); // Java 1.5 (DOM Level 3)
     351        // xmlElements.appendChild(element);
     352
     353        // Java 1.4 equivalent
     354        Node doc_owned_node = doc.importNode(element,true);
     355        // Should also remove element from its original parent, as adopt does?
     356        // element.getParentNode().removeChild(element);
     357
     358        xmlElements.appendChild(doc_owned_node);
     359    }
     360       
     361    page.getPageResponse().appendChild(xmlElements);
     362    }
     363   
     364    protected Node transformPage(Element page) {
     365    return super.transformPage(page);
     366    }
     367   
     368    private HashMap getRelevantElements(String tagName, String lang, Document siteLevel, Document collectLevel) {
     369       
     370    HashMap elements = new HashMap();
     371       
     372    // get all relevant elements from collect level
     373    if (collectLevel != null) {
     374           
     375        NodeList xmlElements = collectLevel.getElementsByTagName(tagName);
     376        for (int di = 0, dn = xmlElements.getLength(); di < dn; di++) {
     377               
     378        Element xmlElement = (Element)xmlElements.item(di);
     379               
     380        // only include elements that are language independent or are the correct language, and do not specify inherit=true
     381        if ((xmlElement.getAttribute("lang").equals("") || xmlElement.getAttribute("lang").equalsIgnoreCase(lang)) && !xmlElement.getAttribute("inherit").equalsIgnoreCase("true")) {
     382            String name = xmlElement.getAttribute("name");
     383            //System.out.println(name + " found and added at collect level");
     384            elements.put(name, xmlElement);
    195385        }   
    196        
    197         // get site level files
    198         String site_home = page.getSiteHome() ;
    199         File dir = new File(site_home + File.separatorChar + "ui" + File.separatorChar + "css") ;
    200         if (dir.isDirectory()) {
    201             File[] files = dir.listFiles() ;
    202             for (int fi=0, fn=files.length ; fi<fn ; fi++) {
    203                 if (files[fi].getName().endsWith(".css")) {
    204                     Element cssFile = doc.createElement("cssFile") ;
    205                     String path = files[fi].getPath() ;
    206                     path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
    207                         //changes to a correct path in case GS3 runs on Windows
    208                         path = path.replace( '\\', '/' );                   
    209                     cssFile.setAttribute("path", path) ;
    210                     cssFileList.appendChild(cssFile) ;
    211                 }
    212             }
    213         }
    214        
    215         // get collect level files
    216         String collect_home = page.getCollectionHome() ;
    217         if (collect_home != "") {
    218             dir = new File(collect_home + File.separatorChar + "ui" + File.separatorChar + "css") ;
    219             if (dir.isDirectory()) {
    220                 File[] files = dir.listFiles() ;
    221                 for (int fi=0, fn=files.length ; fi<fn ; fi++) {
    222                     if (files[fi].getName().endsWith(".css")) {
    223                         Element cssFile = doc.createElement("cssFile") ;
    224                         String path = files[fi].getPath() ;
    225                         path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
    226                             //changes to a correct path in case GS3 runs on Windows
    227                             path = path.replace( '\\', '/' );       
    228                         cssFile.setAttribute("path", path) ;
    229                         cssFileList.appendChild(cssFile) ;
    230                     }
    231                 }
    232             }
    233         }
    234        
    235         page.getPageResponse().appendChild(cssFileList) ;
    236     }
    237    
    238     protected void addJsFiles(Page page, Skin skin) {
    239        
    240         Element jsFileList = doc.createElement("jsFileList") ;
    241        
    242         // get skin level files
    243         if (skin != null) {
    244             File dir = new File(skin.rootDirectory.getPath() + File.separatorChar + "js") ;
    245             if (dir.isDirectory()) {
    246                 File[] files = dir.listFiles() ;
    247                
    248                 for (int fi=0, fn=files.length ; fi<fn ; fi++) {
    249                     if (files[fi].getName().endsWith(".js")) {
    250                         Element jsFile = doc.createElement("jsFile") ;
    251                         String path = files[fi].getPath() ;
    252                         path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
    253                         //changes to a correct path in case GS3 runs on Windows
    254                             path = path.replace( '\\', '/' );   
    255                         jsFile.setAttribute("path", path) ;
    256                         jsFileList.appendChild(jsFile) ;
    257                     }
    258                 }
    259             }           
    260         }   
    261        
    262         // get site level files
    263         String site_home = page.getSiteHome() ;
    264         File dir = new File(site_home + File.separatorChar + "ui" + File.separatorChar + "js") ;
    265         if (dir.isDirectory()) {
    266             File[] files = dir.listFiles() ;
    267             for (int fi=0, fn=files.length ; fi<fn ; fi++) {
    268                 if (files[fi].getName().endsWith(".js")) {
    269                     Element jsFile = doc.createElement("jsFile") ;
    270                     String path = files[fi].getPath() ;
    271                     path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
    272                     //changes to a correct path in case GS3 runs on Windows
    273                             path = path.replace( '\\', '/' );   
    274                     jsFile.setAttribute("path", path) ;
    275                     jsFileList.appendChild(jsFile) ;
    276                 }
    277             }
    278         }
    279        
    280         // get collect level files
    281         String collect_home = page.getCollectionHome() ;
    282         if (collect_home != "") {
    283             dir = new File(collect_home + File.separatorChar + "ui" + File.separatorChar + "js") ;
    284             if (dir.isDirectory()) {
    285                 File[] files = dir.listFiles() ;
    286                 for (int fi=0, fn=files.length ; fi<fn ; fi++) {
    287                     if (files[fi].getName().endsWith(".js")) {
    288                         Element jsFile = doc.createElement("jsFile") ;
    289                         String path = files[fi].getPath() ;
    290                         path = "." + path.substring(GlobalProperties.getGSDL3Home().length()) ;
    291                         //changes to a correct path in case GS3 runs on Windows
    292                             path = path.replace( '\\', '/' );   
    293                         jsFile.setAttribute("path", path) ;
    294                         jsFileList.appendChild(jsFile) ;
    295                     }
    296                 }
    297             }
    298         }
    299        
    300         page.getPageResponse().appendChild(jsFileList) ;
    301     }
    302    
    303     protected void addUiElements(Page page) {
    304        
    305         Document siteUi = page.getSiteUi() ;
    306         Document collectUi = page.getCollectUi() ;
    307         String lang = page.getLanguage() ;
    308        
    309         HashMap<String, Element> elements = getRelevantElements("uiItem", lang, siteUi, collectUi) ;
    310         Element xmlElements = doc.createElement("uiItemList") ;
    311        
    312         Iterator<Element> i = elements.values().iterator() ;
    313         while (i.hasNext()) {
    314             Element element = i.next() ;
    315            
    316             doc.adoptNode(element) ;
    317             xmlElements.appendChild(element) ;
    318            
    319            
    320         }
    321        
    322         page.getPageResponse().appendChild(xmlElements) ;
    323     }
    324    
    325     protected void addMetadataElements(Page page) {
    326         Document siteMetadata = page.getSiteMetadata() ;
    327         Document collectMetadata = page.getCollectMetadata() ;
    328         String lang = page.getLanguage() ;
    329        
    330         HashMap<String, Element> elements = getRelevantElements("metadataItem", lang, siteMetadata, collectMetadata) ;
    331         Element xmlElements = doc.createElement("metadataList") ;
    332        
    333         Iterator<Element> i = elements.values().iterator() ;
    334         while (i.hasNext()) {
    335             Element element = i.next() ;
    336             doc.adoptNode(element) ;
    337             xmlElements.appendChild(element) ;
    338         }
    339        
    340         page.getPageResponse().appendChild(xmlElements) ;
    341     }
    342    
    343     protected Node transformPage(Element page) {
    344         return super.transformPage(page) ;
    345     }
    346    
    347     private HashMap getRelevantElements(String tagName, String lang, Document siteLevel, Document collectLevel) {
    348        
    349         HashMap elements = new HashMap<String, Element>() ;
    350        
    351         // get all relevant elements from collect level
    352         if (collectLevel != null) {
    353            
    354             NodeList xmlElements = collectLevel.getElementsByTagName(tagName) ;
    355             for (int di = 0, dn = xmlElements.getLength() ; di < dn ; di++) {
    356                
    357                 Element xmlElement = (Element)xmlElements.item(di) ;
    358                
    359                 // only include elements that are language independent or are the correct language, and do not specify inherit=true
    360                 if ((xmlElement.getAttribute("lang").equals("") || xmlElement.getAttribute("lang").equalsIgnoreCase(lang)) && !xmlElement.getAttribute("inherit").equalsIgnoreCase("true")) {
    361                     String name = xmlElement.getAttribute("name") ;
    362                     //System.out.println(name + " found and added at collect level") ;
    363                     elements.put(name, xmlElement) ;
    364                 }   
    365             }
    366         }
    367        
    368         // get all elements from site level
    369         if (siteLevel != null) {
    370            
    371             //System.out.println("getting all " + tagName + "'s (" + lang + ") from " + converter.getPrettyString(siteLevel)) ;
    372            
    373             NodeList xmlElements = siteLevel.getElementsByTagName(tagName) ;
    374             for (int di = 0, dn = xmlElements.getLength() ; di < dn ; di++) {
    375                
    376                 Element xmlElement = (Element)xmlElements.item(di) ;
    377                
    378                 // only include elements that are language independent or are the correct language, and which havent already been specified at collect level
    379                 if (xmlElement.getAttribute("lang").equals("") || xmlElement.getAttribute("lang").equalsIgnoreCase(lang)) {
    380                     String name = xmlElement.getAttribute("name") ;
    381                     //System.out.println(name + " found at site level") ;
     386        }
     387    }
     388       
     389    // get all elements from site level
     390    if (siteLevel != null) {
     391           
     392        //System.out.println("getting all " + tagName + "'s (" + lang + ") from " + converter.getPrettyString(siteLevel));
     393           
     394        NodeList xmlElements = siteLevel.getElementsByTagName(tagName);
     395        for (int di = 0, dn = xmlElements.getLength(); di < dn; di++) {
     396               
     397        Element xmlElement = (Element)xmlElements.item(di);
     398               
     399        // only include elements that are language independent or are the correct language, and which havent already been specified at collect level
     400        if (xmlElement.getAttribute("lang").equals("") || xmlElement.getAttribute("lang").equalsIgnoreCase(lang)) {
     401            String name = xmlElement.getAttribute("name");
     402            //System.out.println(name + " found at site level");
    382403                   
    383                     if (!elements.containsKey(name)) {
    384                         elements.put(name, xmlElement) ;
    385                         //System.out.println(name + " added at site level") ;
    386                     }
    387                 }
    388             }
    389         }
    390        
    391         return elements ;
    392     }
    393    
    394    
    395    
    396    
    397    
     404            if (!elements.containsKey(name)) {
     405            elements.put(name, xmlElement);
     406            //System.out.println(name + " added at site level");
     407            }
     408        }
     409        }
     410    }
     411       
     412    return elements;
     413    }
     414   
     415       
    398416}
Note: See TracChangeset for help on using the changeset viewer.