cmailleux    2005/02/16 11:46:18 CET

  Modified files:
    war/src/java/com/codeva/webapps/webclipping Rewriter.java 
  Log:
  Add rewriting of the input image tag
  
  Revision  Changes    Path
  1.3       +250 -213  
webclipping/war/src/java/com/codeva/webapps/webclipping/Rewriter.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/webclipping/war/src/java/com/codeva/webapps/webclipping/Rewriter.java.diff?r1=1.2&r2=1.3&f=h
  
  
  
  Index: Rewriter.java
  ===================================================================
  RCS file: 
/home/cvs/repository/webclipping/war/src/java/com/codeva/webapps/webclipping/Rewriter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Rewriter.java     13 Dec 2004 09:23:11 -0000      1.2
  +++ Rewriter.java     16 Feb 2005 10:46:17 -0000      1.3
  @@ -19,12 +19,96 @@
    * To change this template use File | Settings | File Templates.
    */
   public final class Rewriter {
  +// ------------------------------ FIELDS ------------------------------
  +
       public static final String URL_PATH_PARAM_NAME = 
"jahia_url_web_clipping";
       private boolean isTrunked = false;
  -    private URL urlProperties;
  -    private String url;
  +    private Pattern pattern = null;
       private final String regex = 
"\\.((jpg)|(jpeg)|(gif)|(png)|(xls)|(doc)|(pdf))$";
  -    private Pattern pattern = Pattern.compile(regex);
  +    private String url;
  +    private URL urlProperties;
  +
  +// -------------------------- STATIC METHODS --------------------------
  +
  +    private static StringBuffer getReinitLink(HttpServletResponse response, 
HttpServletRequest httpServletRequest) {
  +        StringBuffer buffer = new StringBuffer(256);
  +        buffer.append("<a href=\"");
  +        buffer.append(response.encodeURL(httpServletRequest.getContextPath() 
+
  +                                         httpServletRequest.getServletPath() 
+
  +                                         "?reinit=true"));
  +        buffer.append("\">Reinit</a>\n<br>");
  +        return buffer;
  +    }
  +
  +    private static OutputDocument moveScriptInNonTrunkedHtml(OutputDocument 
outputDocument) {
  +        // Manage the javascript
  +        Source source = new Source(outputDocument.toString());
  +        List scripts = source.findAllStartTags(Tag.SCRIPT);
  +        StartTag body;
  +        for (int i = scripts.size() - 1; i >= 0; i--) {
  +            StartTag startTag = (StartTag) scripts.get(i);
  +            int begin = startTag.getBegin();
  +            int end;
  +            if (startTag.findEndTag() != null) {
  +                end = startTag.findEndTag().getEnd();
  +            } else {
  +                end = startTag.getEnd();
  +            }
  +            body = (StartTag) source.findAllStartTags(Tag.BODY).get(0);
  +            if (end < body.getBegin()) {
  +                String s = outputDocument.toString();
  +                outputDocument.add(new StringOutputSegment(body,
  +                                                           new 
StringBuffer(4096).append(
  +                                                                   
s.substring(body.getBegin(), body.getEnd())).append(
  +                                                                           
'\n').append(s.substring(begin, end)).toString()));
  +                s = outputDocument.toString();
  +                outputDocument = new OutputDocument(new 
StringBuffer(100000).append(s.substring(0, begin)).append(
  +                        s.substring(end, s.length())).toString());
  +                // Now we have changed the document so we must restart the 
list
  +                i++;
  +                source = new Source(outputDocument.toString());
  +                scripts = source.findAllStartTags(Tag.SCRIPT);
  +            }
  +        }
  +        return outputDocument;
  +    }
  +
  +    private static OutputDocument moveScriptInTrunkedHtml(Source origin, int 
startPos, OutputDocument outputDocument,
  +                                                          final String text, 
int endPos) {
  +        List scripts = origin.findAllStartTags(Tag.SCRIPT);
  +        for (int i = scripts.size() - 1; i >= 0; i--) {
  +            StartTag startTag = (StartTag) scripts.get(i);
  +            int begin = startTag.getBegin();
  +            int end;
  +            if (startTag.findEndTag() != null) {
  +                end = startTag.findEndTag().getEnd();
  +            } else {
  +                end = startTag.getEnd();
  +            }
  +            if (end < startPos) {
  +                // This script isn't enclose in the trunked tag so we must 
move it to ensure everything working
  +                StartTag body = (StartTag) new 
Source(outputDocument.toString()).findAllStartTags().get(0);
  +                String s = outputDocument.toString();
  +                outputDocument.add(new StringOutputSegment(body,
  +                                                           new 
StringBuffer(4096).append(
  +                                                                   
s.substring(body.getBegin(), body.getEnd())).append(
  +                                                                           
'\n').append(text.substring(begin, end)).toString()));
  +                outputDocument = new 
OutputDocument(outputDocument.toString());
  +            } else if (begin > endPos) {
  +                // This script isn't enclose in the trunked tag so we must 
move it to ensure everything working
  +                EndTag body = ((StartTag) new 
Source(outputDocument.toString()).findAllStartTags().get(0)).findEndTag();
  +                String s = outputDocument.toString();
  +                outputDocument.add(new StringOutputSegment(body,
  +                                                           new 
StringBuffer(4096).append(text.substring(begin, end)).append(
  +                                                                   
'\n').append(
  +                                                                           
s.substring(body.getBegin(), body.getEnd())).toString()));
  +                outputDocument = new 
OutputDocument(outputDocument.toString());
  +            }
  +        }
  +        return outputDocument;
  +    }
  +
  +// --------------------------- CONSTRUCTORS ---------------------------
   
       protected Rewriter() {
       }
  @@ -32,8 +116,11 @@
       public Rewriter(String url) throws MalformedURLException {
           this.url = url;
           urlProperties = new URL(url);
  +        pattern = Pattern.compile(regex);
       }
   
  +// -------------------------- OTHER METHODS --------------------------
  +
       /**
        * This method rewrite the url of an html document.
        *
  @@ -61,6 +148,7 @@
           rewriteFormTag(source, stringBuffer, request, response, document);
           rewriteATag(source, stringBuffer, request, response, document);
           rewriteImgTag(source, stringBuffer, document);
  +        rewriteInputImageTag(source, stringBuffer, document);
           rewriteLinkTag(source, stringBuffer, document);
           rewriteFrameTag(source, stringBuffer, document);
           rewriteIFrameTag(source, stringBuffer, document);
  @@ -92,35 +180,84 @@
           }
       }
   
  -    private void rewriteFormTag(Source source, StringBuffer stringBuffer,
  -                                HttpServletRequest httpServletRequest,
  -                                HttpServletResponse response,
  -                                OutputDocument document) throws 
MalformedURLException {
  -        List formStartTag = source.findAllStartTags(Tag.FORM);
  -        for (int i = 0; i < formStartTag.size(); i++) {
  -            StartTag startTag = (StartTag) formStartTag.get(i);
  -            Attributes attributes = startTag.getAttributes();
  -            Attribute action = attributes.get("action");
  -            if (action != null) {
  -                String actionUrl = action.getValue();
  -                stringBuffer.setLength(0);
  -                stringBuffer.append("<form 
action=\"").append(getRewritedUrl(httpServletRequest, response,
  -                                                                             
actionUrl)).append("\" ");
  -                List atList = attributes.getList();
  -                for (int j = 0; j < atList.size(); j++) {
  -                    Attribute attribute = (Attribute) atList.get(j);
  -                    final String name = attribute.getName();
  -                    if (!"action".equalsIgnoreCase(name) && 
!"method".equalsIgnoreCase(name)) {
  -                        
stringBuffer.append(attribute.getSourceText()).append(' ');
  +    private String getAbsoluteURL(String hrefUrl) throws 
MalformedURLException {
  +        final String s = urlProperties.getProtocol() + "://" + 
urlProperties.getHost() +
  +                         ((urlProperties.getPort() >=
  +                           0) ? ":" +
  +                                urlProperties.getPort() : "");
  +        String absoluteUrl = s + urlProperties.getPath().substring(0, 
urlProperties.getPath().lastIndexOf("/")) + '/' +
  +                             hrefUrl;
  +        if (hrefUrl.trim().length() > 0) {
  +            if(hrefUrl.startsWith("//")) {
  +                // We have a net_path accordind to RFC_2396 definig URI
  +                absoluteUrl = urlProperties.getProtocol()+":"+hrefUrl;
  +            }
  +            else if (hrefUrl.charAt(0) == '/') {
  +                // We have an aboslute url
  +                absoluteUrl = s + hrefUrl;
  +            } else if (hrefUrl.startsWith("http")) {
  +                URL tmp = new URL(hrefUrl);
  +                if (tmp.getHost().equalsIgnoreCase(urlProperties.getHost())) 
{
  +                    absoluteUrl = hrefUrl;
  +                }
  +            }
  +        }
  +        return absoluteUrl;
  +    }
  +
  +    /**
  +     * Rewrite the specified url to be a webclipping url.
  +     *
  +     * @param request
  +     * @param response
  +     * @param sourceUrl
  +     *
  +     * @return
  +     */
  +    private String getRewritedUrl(HttpServletRequest request, 
HttpServletResponse response, String sourceUrl)
  +            throws MalformedURLException {
  +        final String rewritedUrl = request.getContextPath() + 
request.getServletPath() + '?' +
  +                                   Rewriter.URL_PATH_PARAM_NAME + '=' + 
getAbsoluteURL(sourceUrl);
  +        return response.encodeURL(rewritedUrl);
  +    }
  +
  +    private OutputDocument getTrunkedDocument(String tag, String 
attributeName, String attributeValue,
  +                                              String content) {
  +        OutputDocument outputDocument = new OutputDocument(content);
  +        Source origin = new Source(content);
  +        int startPos = 0; // Start position of the trunked tag
  +        int endPos = 0; // End position of the trunked tag
  +        if (!"".equals(tag)) {
  +            // Get the list of the specified tag
  +            List tags = origin.findAllStartTags(tag.toLowerCase());
  +            for (int i = 0; i < tags.size(); i++) {
  +                StartTag startTag = (StartTag) tags.get(i);
  +                if (!"".equals(attributeName)) {
  +                    // Get the list of attributes to ensure that is the 
right tag
  +                    Attributes attributes = startTag.getAttributes();
  +                    Attribute attribute = attributes.get(attributeName);
  +                    if 
(attribute.getValue().equalsIgnoreCase(attributeValue)) {
  +                        startPos = startTag.getBegin();
  +                        endPos = startTag.findEndTag().getBegin();
  +                        outputDocument = trunk(content, startTag);
  +                        break;
                       }
  +                } else {
  +                    // Trunk and get only th econtent of the specified tag.
  +                    outputDocument = trunk(content, startTag);
  +                    startPos = startTag.getBegin();
  +                    endPos = startTag.findEndTag().getBegin();
  +                    break;
                   }
  -                Attribute method = attributes.get("method");
  -                stringBuffer.append("method=\"post\">\n");
  -                stringBuffer.append("<input type=\"hidden\" 
name=\"original_method\" value=\"").append(
  -                        (method != null) ? method.getValue() : 
"get").append("\">");
  -                document.add(new StringOutputSegment(startTag, 
stringBuffer.toString()));
               }
           }
  +        outputDocument = new OutputDocument(outputDocument.toString());
  +        if (!isTrunked) {
  +            outputDocument = 
Rewriter.moveScriptInNonTrunkedHtml(outputDocument);
  +        } else {
  +            outputDocument = Rewriter.moveScriptInTrunkedHtml(origin, 
startPos, outputDocument, content, endPos);
  +        }
  +        return outputDocument;
       }
   
       private void rewriteATag(Source source, StringBuffer stringBuffer, 
HttpServletRequest httpServletRequest,
  @@ -138,7 +275,8 @@
                       if (!matcher.find()) {
                           String rewritedUrl = 
getRewritedUrl(httpServletRequest, response,
                                                               
hrefUrl.replaceAll("\\?", "&"));
  -                        if (attributes.get("target") != null) {
  +                        final Attribute target = attributes.get("target");
  +                        if (target != null && !target.equals("_self")) {
                               rewritedUrl = getAbsoluteURL(hrefUrl);
                           }
                           stringBuffer.append("<a 
href=\"").append(rewritedUrl).append("\" ");
  @@ -225,9 +363,40 @@
           }
       }
   
  -    private void rewriteImgTag(Source source, StringBuffer stringBuffer, 
OutputDocument document)
  +    private void rewriteFormTag(Source source, StringBuffer stringBuffer,
  +                                HttpServletRequest httpServletRequest,
  +                                HttpServletResponse response,
  +                                OutputDocument document) throws 
MalformedURLException {
  +        List formStartTag = source.findAllStartTags(Tag.FORM);
  +        for (int i = 0; i < formStartTag.size(); i++) {
  +            StartTag startTag = (StartTag) formStartTag.get(i);
  +            Attributes attributes = startTag.getAttributes();
  +            Attribute action = attributes.get("action");
  +            if (action != null) {
  +                String actionUrl = action.getValue();
  +                stringBuffer.setLength(0);
  +                stringBuffer.append("<form 
action=\"").append(getRewritedUrl(httpServletRequest, response,
  +                                                                             
actionUrl)).append("\" ");
  +                List atList = attributes.getList();
  +                for (int j = 0; j < atList.size(); j++) {
  +                    Attribute attribute = (Attribute) atList.get(j);
  +                    final String name = attribute.getName();
  +                    if (!"action".equalsIgnoreCase(name) && 
!"method".equalsIgnoreCase(name)) {
  +                        
stringBuffer.append(attribute.getSourceText()).append(' ');
  +                    }
  +                }
  +                Attribute method = attributes.get("method");
  +                stringBuffer.append("method=\"post\">\n");
  +                stringBuffer.append("<input type=\"hidden\" 
name=\"original_method\" value=\"").append(
  +                        (method != null) ? method.getValue() : 
"get").append("\">");
  +                document.add(new StringOutputSegment(startTag, 
stringBuffer.toString()));
  +            }
  +        }
  +    }
  +
  +    private void rewriteFrameTag(Source source, StringBuffer stringBuffer, 
OutputDocument document)
               throws MalformedURLException {
  -        List aStartTag = source.findAllStartTags(Tag.IMG);
  +        List aStartTag = source.findAllStartTags(Tag.FRAME);
           for (int i = 0; i < aStartTag.size(); i++) {
               StartTag startTag = (StartTag) aStartTag.get(i);
               Attributes attributes = startTag.getAttributes();
  @@ -236,7 +405,7 @@
                   String hrefUrl = href.getValue().trim();
                   stringBuffer.setLength(0);
                   if (!hrefUrl.startsWith("http")) {
  -                    stringBuffer.append("<img 
src=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
  +                    stringBuffer.append("<frame 
src=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
                       List atList = attributes.getList();
                       for (int j = 0; j < atList.size(); j++) {
                           Attribute attribute = (Attribute) atList.get(j);
  @@ -245,15 +414,16 @@
                           }
                       }
                       stringBuffer.append('>');
  -                    document.add(new StringOutputSegment(startTag, 
stringBuffer.toString()));
  +                    document.add(new StringOutputSegment(startTag,
  +                                                         
stringBuffer.toString()));
                   }
               }
           }
       }
   
  -    private void rewriteScriptTag(Source source, StringBuffer stringBuffer, 
OutputDocument document)
  +    private void rewriteIFrameTag(Source source, StringBuffer stringBuffer, 
OutputDocument document)
               throws MalformedURLException {
  -        List aStartTag = source.findAllStartTags(Tag.SCRIPT);
  +        List aStartTag = source.findAllStartTags(Tag.IFRAME);
           for (int i = 0; i < aStartTag.size(); i++) {
               StartTag startTag = (StartTag) aStartTag.get(i);
               Attributes attributes = startTag.getAttributes();
  @@ -262,7 +432,7 @@
                   String hrefUrl = href.getValue().trim();
                   stringBuffer.setLength(0);
                   if (!hrefUrl.startsWith("http")) {
  -                    stringBuffer.append("<script 
src=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
  +                    stringBuffer.append("<iframe 
src=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
                       List atList = attributes.getList();
                       for (int j = 0; j < atList.size(); j++) {
                           Attribute attribute = (Attribute) atList.get(j);
  @@ -278,9 +448,9 @@
           }
       }
   
  -    private void rewriteFrameTag(Source source, StringBuffer stringBuffer, 
OutputDocument document)
  +    private void rewriteImgTag(Source source, StringBuffer stringBuffer, 
OutputDocument document)
               throws MalformedURLException {
  -        List aStartTag = source.findAllStartTags(Tag.FRAME);
  +        List aStartTag = source.findAllStartTags(Tag.IMG);
           for (int i = 0; i < aStartTag.size(); i++) {
               StartTag startTag = (StartTag) aStartTag.get(i);
               Attributes attributes = startTag.getAttributes();
  @@ -289,7 +459,7 @@
                   String hrefUrl = href.getValue().trim();
                   stringBuffer.setLength(0);
                   if (!hrefUrl.startsWith("http")) {
  -                    stringBuffer.append("<frame 
src=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
  +                    stringBuffer.append("<img 
src=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
                       List atList = attributes.getList();
                       for (int j = 0; j < atList.size(); j++) {
                           Attribute attribute = (Attribute) atList.get(j);
  @@ -298,25 +468,25 @@
                           }
                       }
                       stringBuffer.append('>');
  -                    document.add(new StringOutputSegment(startTag,
  -                                                         
stringBuffer.toString()));
  +                    document.add(new StringOutputSegment(startTag, 
stringBuffer.toString()));
                   }
               }
           }
       }
   
  -    private void rewriteIFrameTag(Source source, StringBuffer stringBuffer, 
OutputDocument document)
  +    private void rewriteInputImageTag(Source source, StringBuffer 
stringBuffer, OutputDocument document)
               throws MalformedURLException {
  -        List aStartTag = source.findAllStartTags(Tag.IFRAME);
  +        List aStartTag = source.findAllStartTags(Tag.INPUT);
           for (int i = 0; i < aStartTag.size(); i++) {
               StartTag startTag = (StartTag) aStartTag.get(i);
               Attributes attributes = startTag.getAttributes();
               Attribute href = attributes.get("src");
  -            if (href != null) {
  +            Attribute type = attributes.get("type");
  +            if (type != null && "image".equalsIgnoreCase(type.getValue()) && 
href != null) {
                   String hrefUrl = href.getValue().trim();
                   stringBuffer.setLength(0);
                   if (!hrefUrl.startsWith("http")) {
  -                    stringBuffer.append("<iframe 
src=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
  +                    stringBuffer.append("<input 
src=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
                       List atList = attributes.getList();
                       for (int j = 0; j < atList.size(); j++) {
                           Attribute attribute = (Attribute) atList.get(j);
  @@ -325,8 +495,7 @@
                           }
                       }
                       stringBuffer.append('>');
  -                    document.add(new StringOutputSegment(startTag,
  -                                                         
stringBuffer.toString()));
  +                    document.add(new StringOutputSegment(startTag, 
stringBuffer.toString()));
                   }
               }
           }
  @@ -359,20 +528,40 @@
           }
       }
   
  -    /**
  -     * Rewrite the specified url to be a webclipping url.
  -     *
  -     * @param request
  -     * @param response
  -     * @param sourceUrl
  -     *
  -     * @return
  -     */
  -    private String getRewritedUrl(HttpServletRequest request, 
HttpServletResponse response, String sourceUrl)
  +    private void rewriteScriptTag(Source source, StringBuffer stringBuffer, 
OutputDocument document)
               throws MalformedURLException {
  -        final String rewritedUrl = request.getContextPath() + 
request.getServletPath() + '?' +
  -                                   Rewriter.URL_PATH_PARAM_NAME + '=' + 
getAbsoluteURL(sourceUrl);
  -        return response.encodeURL(rewritedUrl);
  +        List aStartTag = source.findAllStartTags(Tag.SCRIPT);
  +        for (int i = 0; i < aStartTag.size(); i++) {
  +            StartTag startTag = (StartTag) aStartTag.get(i);
  +            Attributes attributes = startTag.getAttributes();
  +            Attribute href = attributes.get("src");
  +            if (href != null) {
  +                String hrefUrl = href.getValue().trim();
  +                stringBuffer.setLength(0);
  +                if (!hrefUrl.startsWith("http")) {
  +                    stringBuffer.append("<script 
src=\"").append(getAbsoluteURL(hrefUrl)).append("\" ");
  +                    List atList = attributes.getList();
  +                    for (int j = 0; j < atList.size(); j++) {
  +                        Attribute attribute = (Attribute) atList.get(j);
  +                        if (!"src".equalsIgnoreCase(attribute.getName())) {
  +                            
stringBuffer.append(attribute.getSourceText()).append(' ');
  +                        }
  +                    }
  +                    stringBuffer.append('>');
  +                    document.add(new StringOutputSegment(startTag,
  +                                                         
stringBuffer.toString()));
  +                }
  +            }
  +        }
  +    }
  +
  +    private OutputDocument trunk(String document,
  +                                 StartTag startTag) {
  +        StringBuffer buffer = new StringBuffer(document.length());
  +        buffer.append(document.substring(startTag.getBegin(),
  +                                         startTag.findEndTag().getEnd()));
  +        isTrunked = true;
  +        return new OutputDocument(buffer.toString());
       }
   
       /**
  @@ -399,157 +588,5 @@
           outputDocument = getTrunkedDocument(tag, attributeName, 
attributeValue, outputDocument.toString());
           return outputDocument;
       }
  -
  -    private OutputDocument getTrunkedDocument(String tag, String 
attributeName, String attributeValue,
  -                                              String content) {
  -        OutputDocument outputDocument = new OutputDocument(content);
  -        Source origin = new Source(content);
  -        int startPos = 0; // Start position of the trunked tag
  -        int endPos = 0; // End position of the trunked tag
  -        if (!"".equals(tag)) {
  -            // Get the list of the specified tag
  -            List tags = origin.findAllStartTags(tag.toLowerCase());
  -            for (int i = 0; i < tags.size(); i++) {
  -                StartTag startTag = (StartTag) tags.get(i);
  -                if (!"".equals(attributeName)) {
  -                    // Get the list of attributes to ensure that is the 
right tag
  -                    Attributes attributes = startTag.getAttributes();
  -                    Attribute attribute = attributes.get(attributeName);
  -                    if 
(attribute.getValue().equalsIgnoreCase(attributeValue)) {
  -                        startPos = startTag.getBegin();
  -                        endPos = startTag.findEndTag().getBegin();
  -                        outputDocument = trunk(content, startTag);
  -                        break;
  -                    }
  -                } else {
  -                    // Trunk and get only th econtent of the specified tag.
  -                    outputDocument = trunk(content, startTag);
  -                    startPos = startTag.getBegin();
  -                    endPos = startTag.findEndTag().getBegin();
  -                    break;
  -                }
  -            }
  -        }
  -        outputDocument = new OutputDocument(outputDocument.toString());
  -        if (!isTrunked) {
  -            outputDocument = 
Rewriter.moveScriptInNonTrunkedHtml(outputDocument);
  -        } else {
  -            outputDocument = Rewriter.moveScriptInTrunkedHtml(origin, 
startPos, outputDocument, content, endPos);
  -        }
  -        return outputDocument;
  -    }
  -
  -    private static OutputDocument moveScriptInTrunkedHtml(Source origin, int 
startPos, OutputDocument outputDocument,
  -                                                          final String text, 
int endPos) {
  -        List scripts = origin.findAllStartTags(Tag.SCRIPT);
  -        for (int i = scripts.size() - 1; i >= 0; i--) {
  -            StartTag startTag = (StartTag) scripts.get(i);
  -            int begin = startTag.getBegin();
  -            int end;
  -            if (startTag.findEndTag() != null) {
  -                end = startTag.findEndTag().getEnd();
  -            } else {
  -                end = startTag.getEnd();
  -            }
  -            if (end < startPos) {
  -                // This script isn't enclose in the trunked tag so we must 
move it to ensure everything working
  -                StartTag body = (StartTag) new 
Source(outputDocument.toString()).findAllStartTags().get(0);
  -                String s = outputDocument.toString();
  -                outputDocument.add(new StringOutputSegment(body,
  -                                                           new 
StringBuffer(4096).append(
  -                                                                   
s.substring(body.getBegin(), body.getEnd())).append(
  -                                                                           
'\n').append(text.substring(begin, end)).toString()));
  -                outputDocument = new 
OutputDocument(outputDocument.toString());
  -            } else if (begin > endPos) {
  -                // This script isn't enclose in the trunked tag so we must 
move it to ensure everything working
  -                EndTag body = ((StartTag) new 
Source(outputDocument.toString()).findAllStartTags().get(0)).findEndTag();
  -                String s = outputDocument.toString();
  -                outputDocument.add(new StringOutputSegment(body,
  -                                                           new 
StringBuffer(4096).append(text.substring(begin, end)).append(
  -                                                                   
'\n').append(
  -                                                                           
s.substring(body.getBegin(), body.getEnd())).toString()));
  -                outputDocument = new 
OutputDocument(outputDocument.toString());
  -            }
  -        }
  -        return outputDocument;
  -    }
  -
  -    private static OutputDocument moveScriptInNonTrunkedHtml(OutputDocument 
outputDocument) {
  -        // Manage the javascript
  -        Source source = new Source(outputDocument.toString());
  -        List scripts = source.findAllStartTags(Tag.SCRIPT);
  -        StartTag body;
  -        for (int i = scripts.size() - 1; i >= 0; i--) {
  -            StartTag startTag = (StartTag) scripts.get(i);
  -            int begin = startTag.getBegin();
  -            int end;
  -            if (startTag.findEndTag() != null) {
  -                end = startTag.findEndTag().getEnd();
  -            } else {
  -                end = startTag.getEnd();
  -            }
  -            body = (StartTag) source.findAllStartTags(Tag.BODY).get(0);
  -            if (end < body.getBegin()) {
  -                String s = outputDocument.toString();
  -                outputDocument.add(new StringOutputSegment(body,
  -                                                           new 
StringBuffer(4096).append(
  -                                                                   
s.substring(body.getBegin(), body.getEnd())).append(
  -                                                                           
'\n').append(s.substring(begin, end)).toString()));
  -                s = outputDocument.toString();
  -                outputDocument = new OutputDocument(new 
StringBuffer(100000).append(s.substring(0, begin)).append(
  -                        s.substring(end, s.length())).toString());
  -                // Now we have changed the document so we must restart the 
list
  -                i++;
  -                source = new Source(outputDocument.toString());
  -                scripts = source.findAllStartTags(Tag.SCRIPT);
  -            }
  -        }
  -        return outputDocument;
  -    }
  -
  -    private OutputDocument trunk(String document,
  -                                 StartTag startTag) {
  -        StringBuffer buffer = new StringBuffer(document.length());
  -        buffer.append(document.substring(startTag.getBegin(),
  -                                         startTag.findEndTag().getEnd()));
  -        isTrunked = true;
  -        return new OutputDocument(buffer.toString());
  -    }
  -
  -    private static StringBuffer getReinitLink(HttpServletResponse response, 
HttpServletRequest httpServletRequest) {
  -        StringBuffer buffer = new StringBuffer(256);
  -        buffer.append("<a href=\"");
  -        buffer.append(response.encodeURL(httpServletRequest.getContextPath() 
+
  -                                         httpServletRequest.getServletPath() 
+
  -                                         "?reinit=true"));
  -        buffer.append("\">Reinit</a>\n<br>");
  -        return buffer;
  -    }
  -
  -
  -    private String getAbsoluteURL(String hrefUrl) throws 
MalformedURLException {
  -        final String s = urlProperties.getProtocol() + "://" + 
urlProperties.getHost() +
  -                         ((urlProperties.getPort() >=
  -                           0) ? ":" +
  -                                urlProperties.getPort() : "");
  -        String absoluteUrl = s + urlProperties.getPath().substring(0, 
urlProperties.getPath().lastIndexOf("/")) + '/' +
  -                             hrefUrl;
  -        if (hrefUrl.trim().length() > 0) {
  -            if(hrefUrl.startsWith("//")) {
  -                // We have a net_path accordind to RFC_2396 definig URI
  -                absoluteUrl = urlProperties.getProtocol()+":"+hrefUrl;
  -            }
  -            else if (hrefUrl.charAt(0) == '/') {
  -                // We have an aboslute url
  -                absoluteUrl = s + hrefUrl;
  -            } else if (hrefUrl.startsWith("http")) {
  -                URL tmp = new URL(hrefUrl);
  -                if (tmp.getHost().equalsIgnoreCase(urlProperties.getHost())) 
{
  -                    absoluteUrl = hrefUrl;
  -                }
  -            }
  -        }
  -        return absoluteUrl;
  -    }
  -
   }
  +
  

Reply via email to