Changeset 8926


Ignore:
Timestamp:
2005-01-21T15:17:22+13:00 (19 years ago)
Author:
kjdon
Message:

a bit of a hack to get images workign. when loading in an HTML document, we go throught eh images and make them _httpollection_ plus path relative to the collection dir

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/HTMLDocument.java

    r7466 r8926  
    1313import org.greenstone.gsdl3.gs3build.util.*;
    1414import org.greenstone.gsdl3.gs3build.xpointer.XPointer;
     15import org.greenstone.gsdl3.util.XMLConverter;
    1516
    1617import org.w3c.dom.*;
     
    3536  { super(url);
    3637
    37     this.loadDocument(url);
     38    this.loadDocument(url, false);
    3839
    3940    this._extractDocumentFiles();
    4041    this._extractDocumentMetadata();
    41 
     42    // extract the files before resolving the images
     43    resolveImages();
    4244    HTMLDocumentTools docTools = new HTMLDocumentTools(this.domDocument);
    4345    docTools.setMetsDocument(this);
     
    5557  {
    5658    //    HTMLDoc htmlDoc;
     59
    5760    HTMLTidy tidyDoc;
    5861    if (url.getProtocol().equals("file"))
     
    6770
    6871    this.domDocument = tidyDoc.getDocument();
    69 
    7072    Runtime.getRuntime().gc();
    7173    //    System.out.println(Runtime.getRuntime().freeMemory() + " " + Runtime.getRuntime().totalMemory());
     
    172174    continue;
    173175      }
    174 
    175176      try
    176177      { // make the url for the image, and then add it to the document list of
     
    185186    }
    186187
     188 
    187189    /**   
    188190    HTMLBlock codedContent = htmlDoc.getCodedContent();
     
    214216  }
    215217
     218    // I think this is used for single section documents, while getSectionText
     219    // is used for sectioned documents
     220    // we will use the domDocument rather than reading it in again to another HTMLDoc.
    216221  public String getDocumentText()
    217222  {
    218     HTMLDoc htmlDoc;
     223      XMLConverter converter = new XMLConverter();
     224      return converter.getPrettyString(this.domDocument.getDocumentElement());
     225      /*  HTMLDoc htmlDoc;
    219226    URL     url =(URL) this.fileSet.getFile(0).getLocation();
    220227
     
    228235    }
    229236    return htmlDoc.getContent();
    230   }
    231 
    232   public Document getDOMDocument()
    233   {
     237      */
     238  }
     239
     240    private void resolveImages() {
     241
     242    // find the path of the url relative to the collection
     243    URL full_path = this.fileSet.getFile(0).getLocation();
     244       
     245    String base_url;
     246    if (full_path.getProtocol().equals("file")) {
     247        base_url = full_path.getPath();
     248        int import_pos = base_url.indexOf("import");
     249        base_url = base_url.substring(import_pos);
     250        base_url = "_httpcollection_/"+base_url;
     251    } else {
     252        base_url = full_path.toString();
     253    }
     254
     255    // need to take off the last part
     256    base_url = base_url.substring(0, base_url.lastIndexOf("/")+1);
     257
     258    NodeList metadata = this.domDocument.getElementsByTagName("img");
     259    for (int n = 0; n < metadata.getLength(); n ++) {
     260        Node node = metadata.item(n);
     261        Element element = (Element) node;
     262       
     263        String location = element.getAttribute("src");
     264        if (location != null && location.length() > 0 && isRelative(location)) {
     265        // modify the source url
     266        element.setAttribute("src", base_url+location);
     267        }
     268    }
     269    }
     270
     271    private boolean isRelative(String location) {
     272
     273    if ( location.startsWith("http:") || location.startsWith("file:")) {
     274        return false;
     275    }
     276    return true;
     277    }
     278   
     279    public Document getDOMDocument()
     280    {   
    234281    if (this.domDocument == null) {
    235282      URL     url =(URL) this.fileSet.getFile(0).getLocation();
    236283      this.loadDocument(url);
     284      resolveImages();
    237285    }
    238286    return this.domDocument;
     
    269317      URL     url =(URL) this.fileSet.getFile(0).getLocation();
    270318      this.loadDocument(url);
     319      resolveImages();
    271320    }
    272321
Note: See TracChangeset for help on using the changeset viewer.