Ignore:
Timestamp:
2012-07-26T11:02:32+12:00 (12 years ago)
Author:
sjm84
Message:

Analyse xsl files in advance to find out what metadata we need

File:
1 edited

Legend:

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

    r25999 r26025  
    6161    protected DOMParser parser = null;
    6262
     63    protected HashMap<String, ArrayList<String>> _metadataRequiredMap = new HashMap<String, ArrayList<String>>();
     64
    6365    boolean _debug = false;
    6466
     
    9799    public boolean configure()
    98100    {
    99 
    100101        if (this.config_params == null)
    101102        {
     
    180181        }
    181182
     183        GetRequiredMetadataNamesFromXSLFiles();
     184
    182185        return true;
     186    }
     187
     188    protected void GetRequiredMetadataNamesFromXSLFiles()
     189    {
     190        ArrayList<File> xslFiles = GSFile.getAllXSLFiles((String) this.config_params.get(GSConstants.INTERFACE_NAME), (String) this.config_params.get(GSConstants.SITE_NAME));
     191
     192        HashMap<String, ArrayList<String>> includes = new HashMap<String, ArrayList<String>>();
     193        HashMap<String, ArrayList<File>> files = new HashMap<String, ArrayList<File>>();
     194        HashMap<String, ArrayList<String>> metaNames = new HashMap<String, ArrayList<String>>();
     195
     196        //First exploratory pass
     197        for (File currentFile : xslFiles)
     198        {
     199            Document currentDoc = this.converter.getDOM(currentFile);
     200            NodeList metadataElems = currentDoc.getElementsByTagNameNS("http://www.greenstone.org/greenstone3/schema/ConfigFormat", "metadata"); //gsf:metadata
     201            NodeList imageElems = currentDoc.getElementsByTagNameNS("http://www.greenstone.org/greenstone3/schema/ConfigFormat", "image"); //gsf:image
     202            NodeList includeElems = currentDoc.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "include");
     203            NodeList importElems = currentDoc.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform", "import");
     204
     205            ArrayList<String> names = new ArrayList<String>();
     206            for (int i = 0; i < metadataElems.getLength(); i++)
     207            {
     208                Element current = (Element) metadataElems.item(i);
     209                String name = current.getAttribute(GSXML.NAME_ATT);
     210                if (name != null && name.length() > 0 && !names.contains(name))
     211                {
     212                    names.add(name);
     213                }
     214            }
     215
     216            for (int i = 0; i < imageElems.getLength(); i++)
     217            {
     218                Element current = (Element) imageElems.item(i);
     219                String type = current.getAttribute(GSXML.TYPE_ATT);
     220                if (type == null || type.length() == 0)
     221                {
     222                    continue;
     223                }
     224
     225                if (type.equals("source"))
     226                {
     227                    String[] standardSourceMeta = new String[] { "SourceFile", "ImageHeight", "ImageWidth", "ImageType", "srcicon" };
     228                    for (String meta : standardSourceMeta)
     229                    {
     230                        if (!names.contains(meta))
     231                        {
     232                            names.add(meta);
     233                        }
     234                    }
     235                }
     236                else if (type.equals("screen"))
     237                {
     238                    String[] standardScreenMeta = new String[] { "Screen", "ScreenHeight", "ScreenWidth", "ScreenType", "screenicon" };
     239                    for (String meta : standardScreenMeta)
     240                    {
     241                        if (!names.contains(meta))
     242                        {
     243                            names.add(meta);
     244                        }
     245                    }
     246                }
     247                else if (type.equals("thumb"))
     248                {
     249                    String[] standardThumbMeta = new String[] { "Thumb", "ThumbHeight", "ThumbWidth", "ThumbType", "thumbicon" };
     250                    for (String meta : standardThumbMeta)
     251                    {
     252                        if (!names.contains(meta))
     253                        {
     254                            names.add(meta);
     255                        }
     256                    }
     257                }
     258            }
     259
     260            metaNames.put(currentFile.getAbsolutePath(), names);
     261
     262            ArrayList<String> includeAndImportList = new ArrayList<String>();
     263            for (int i = 0; i < includeElems.getLength(); i++)
     264            {
     265                includeAndImportList.add(((Element) includeElems.item(i)).getAttribute(GSXML.HREF_ATT));
     266            }
     267            for (int i = 0; i < importElems.getLength(); i++)
     268            {
     269                includeAndImportList.add(((Element) importElems.item(i)).getAttribute(GSXML.HREF_ATT));
     270            }
     271            includes.put(currentFile.getAbsolutePath(), includeAndImportList);
     272
     273            String filename = currentFile.getName();
     274            if (files.get(filename) == null)
     275            {
     276                ArrayList<File> fileList = new ArrayList<File>();
     277                fileList.add(currentFile);
     278                files.put(currentFile.getName(), fileList);
     279            }
     280            else
     281            {
     282                ArrayList<File> fileList = files.get(filename);
     283                fileList.add(currentFile);
     284            }
     285        }
     286
     287        //Second pass
     288        for (File currentFile : xslFiles)
     289        {
     290            ArrayList<File> filesToGet = new ArrayList<File>();
     291            filesToGet.add(currentFile);
     292
     293            ArrayList<String> fullNameList = new ArrayList<String>();
     294
     295            while (filesToGet.size() > 0)
     296            {
     297                File currentFileTemp = filesToGet.remove(0);
     298
     299                //Add the names from this file
     300                ArrayList<String> currentNames = metaNames.get(currentFileTemp.getAbsolutePath());
     301                fullNameList.addAll(currentNames);
     302
     303                ArrayList<String> includedHrefs = includes.get(currentFileTemp.getAbsolutePath());
     304
     305                for (String href : includedHrefs)
     306                {
     307                    int lastSepIndex = href.lastIndexOf("/");
     308                    if (lastSepIndex != -1)
     309                    {
     310                        href = href.substring(lastSepIndex + 1);
     311                    }
     312
     313                    ArrayList<File> filesToAdd = files.get(href);
     314                    if (filesToAdd != null)
     315                    {
     316                        filesToGet.addAll(filesToAdd);
     317                    }
     318                }
     319            }
     320
     321            _metadataRequiredMap.put(currentFile.getAbsolutePath(), fullNameList);
     322        }
     323    }
     324
     325    protected void preProcessRequest(Element request)
     326    {
     327        String action = request.getAttribute(GSXML.ACTION_ATT);
     328        String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
     329
     330        String name = null;
     331        if (!subaction.equals(""))
     332        {
     333            String key = action + ":" + subaction;
     334            name = this.xslt_map.get(key);
     335        }
     336        // try the action by itself
     337        if (name == null)
     338        {
     339            name = this.xslt_map.get(action);
     340        }
     341
     342        String stylesheetFile = GSFile.interfaceStylesheetFile(GlobalProperties.getGSDL3Home(), (String) this.config_params.get(GSConstants.INTERFACE_NAME), name);
     343        stylesheetFile = stylesheetFile.replace("/", File.separator);
     344
     345        ArrayList<String> requiredMetadata = _metadataRequiredMap.get(stylesheetFile);
     346
     347        Element extraMetadataList = this.doc.createElement(GSXML.EXTRA_METADATA + GSXML.LIST_MODIFIER);
     348
     349        for (String metadataString : requiredMetadata)
     350        {
     351            Element metadataElem = this.doc.createElement(GSXML.EXTRA_METADATA);
     352            metadataElem.setAttribute(GSXML.NAME_ATT, metadataString);
     353            extraMetadataList.appendChild(metadataElem);
     354        }
     355
     356        request.appendChild(request.getOwnerDocument().importNode(extraMetadataList, true));
    183357    }
    184358
     
    231405        else if (excerptTag != null)
    232406        {
    233             /*
    234              * // define a list
    235              *
    236              * Node selectedElement =
    237              * modifyNodesByTagRecursive(transformed_page, excerptTag);
    238              */
    239 
    240407            Node selectedElement = getNodeByTagRecursive(transformed_page, excerptTag);
    241408            return selectedElement;
    242 
    243409        }
    244410        return transformed_page;
     
    296462            if ((result = modifyNodesByTagRecursive(children.item(i), tag)) != null)
    297463            {
    298                 //return result;
    299                 //logger.error("Modify node value = "+result.getNodeValue()); //NamedItem("href"););
    300                 //logger.error("BEFORE Modify node attribute = " + result.getAttributes().getNamedItem("href").getNodeValue());
    301                 //String url = result.getAttributes().getNamedItem("href").getNodeValue();
    302                 //url = url + "&excerptid=results";
    303                 //result.getAttributes().getNamedItem("href").setNodeValue(url);
    304                 //logger.error("AFTER Modify node attribute = " + result.getAttributes().getNamedItem("href").getNodeValue());
     464                //TODO: DO SOMETHING HERE?
    305465            }
    306466        }
     
    583743        {
    584744            String gsLibFile = this.getGSLibXSLFilename();
    585             if(new File(gsLibFile).exists()) {
     745            if (new File(gsLibFile).exists())
     746            {
    586747                libraryXsl = getDoc(gsLibFile);
    587748                String errMsg = ((XMLConverter.ParseErrorHandler) parser.getErrorHandler()).getErrorMessage();
     
    622783
    623784            Element l = skinAndLibraryXsl.createElement("libraryXsl");
    624             if(libraryXsl  != null) {
     785            if (libraryXsl != null)
     786            {
    625787                Element libraryXsl_el = libraryXsl.getDocumentElement();
    626788                l.appendChild(skinAndLibraryXsl.importNode(libraryXsl_el, true));
    627             }   
     789            }
    628790            root.appendChild(l);
    629            
     791
    630792            //System.out.println("Skin and Library XSL are now together") ;
    631793
     
    8741036        return finalDoc;
    8751037    }
    876     //  // now find the absolute path
    877     //  ArrayList<File> stylesheets = GSFile.getStylesheetFiles(GlobalProperties.getGSDL3Home(), (String) this.config_params.get(GSConstants.SITE_NAME), collection, (String) this.config_params.get(GSConstants.INTERFACE_NAME), base_interfaces, name);
    878     //  if (stylesheets.size() == 0)
    879     //  {
    880     //      logger.error(" Can't find stylesheet for " + name);
    881     //      return null;
    882     //  }
    883     //  logger.debug("Stylesheet: " + name);
    884 
    885     //  Document finalDoc = this.converter.getDOM(stylesheets.get(stylesheets.size() - 1), "UTF-8");
    886     //  if (finalDoc == null)
    887     //  {
    888     //      return null;
    889     //  }
    890 
    891     //  for (int i = stylesheets.size() - 2; i >= 0; i--)
    892     //  {
    893     //      Document currentDoc = this.converter.getDOM(stylesheets.get(i), "UTF-8");
    894     //      if (currentDoc == null)
    895     //      {
    896     //          return null;
    897     //      }
    898 
    899     //      if (_debug)
    900     //      {
    901     //          GSXSLT.mergeStylesheetsDebug(finalDoc, currentDoc.getDocumentElement(), true, true, stylesheets.get(stylesheets.size() - 1).getAbsolutePath(), stylesheets.get(i).getAbsolutePath());
    902     //      }
    903     //      else
    904     //      {
    905     //          GSXSLT.mergeStylesheets(finalDoc, currentDoc.getDocumentElement(), true);
    906     //      }
    907     //  }
    908 
    909     //  return finalDoc;
    910     // }
    9111038
    9121039    // returns the path to the gslib.xsl file that is applicable for the current interface
Note: See TracChangeset for help on using the changeset viewer.