xlawrence    2005/06/21 16:10:58 CEST

  Modified files:
    core/src/java/org/jahia/data/fields JahiaBigTextField.java 
  Log:
  Updated field implementation so it can deal with hardcoded links. The field 
now stores internal links with a marker and is able to replace it with the 
correct information at loading time so links can be followed by a browser
  
  Revision  Changes    Path
  1.11      +137 -62   
jahia/core/src/java/org/jahia/data/fields/JahiaBigTextField.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/data/fields/JahiaBigTextField.java.diff?r1=1.10&r2=1.11&f=h
  
  
  
  Index: JahiaBigTextField.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/data/fields/JahiaBigTextField.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- JahiaBigTextField.java    15 Jun 2005 13:04:49 -0000      1.10
  +++ JahiaBigTextField.java    21 Jun 2005 14:10:57 -0000      1.11
  @@ -21,6 +21,8 @@
   import org.jahia.utils.JahiaTools;
   import org.apache.commons.lang.StringUtils;
   
  +import java.util.List;
  +import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.StringTokenizer;
  @@ -33,53 +35,17 @@
       private static org.apache.log4j.Logger logger =
               org.apache.log4j.Logger.getLogger(JahiaBigTextField.class);
       
  -    protected StringBuffer buff;
  +    protected final StringBuffer buff = new StringBuffer();
       
       public static final String URL_MARKER = "###";
       
       public static String SERVLET_PATH;
       
  -    protected void setServletPath(ParamBean jParams)
  -    throws JahiaException {
  -        
  -        if (SERVLET_PATH == null) { // Only perform this operation once
  -            logger.debug("Setting the SERVLET_PATH...");
  -            String path = jParams.settings().getPathResolver().
  -                    resolvePath("/WEB-INF/web.xml");
  -            Web_App_Xml webXmlDoc = new Web_App_Xml(path);
  -            webXmlDoc.extractDocumentData();
  -            
  -            HashMap mappings = webXmlDoc.getServletMappings();
  -            
  -            // Default settings
  -            if (mappings.containsKey("/Jahia/*")) {
  -                SERVLET_PATH = "/Jahia";
  -                return;
  -            }
  -            
  -            Iterator ite = mappings.keySet().iterator();
  -            while (ite.hasNext()) {
  -                String urlPattern = (String)ite.next();
  -                String servletName = (String)mappings.get(urlPattern);
  -                
  -                if (servletName.equals("Jahia") && urlPattern.indexOf("do") 
< 0) {
  -                    SERVLET_PATH = urlPattern.substring(0,
  -                            urlPattern.lastIndexOf('/'));
  -                    return;
  -                }
  -            }
  -            
  -            throw new JahiaException("Unable to set the ServletPath",
  -                    "Unable to set the ServletPath",
  -                    JahiaException.CONFIG_ERROR,
  -                    JahiaException.CRITICAL_SEVERITY);
  -        }
  -    }
  +    // Holds a list of 'pid' as Integer objects
  +    private final List internalLinks;
       
       /**
        * constructor
  -     * YG    17.07.2001
  -     *
        */
       public JahiaBigTextField(Integer ID,
               Integer jahiaID,
  @@ -100,7 +66,7 @@
           if (isShared()) {
               this.languageCode = ContentField.SHARED_LANGUAGE;
           }
  -        buff = new StringBuffer();
  +        internalLinks = new ArrayList();
       } // end constructor
       
       public void load(int loadFlag, ParamBean jParams, EntryLoadRequest 
loadRequest)
  @@ -108,7 +74,7 @@
           
           logger.debug("Loading big text field...");
           setServletPath(jParams);
  -        ContentBigTextField contentBigTextField = (ContentBigTextField)
  +        final ContentBigTextField contentBigTextField = (ContentBigTextField)
           ContentBigTextField.getField(getID());
           
           String val = null;
  @@ -203,18 +169,22 @@
            */
       }
       
  +    /**
  +     *
  +     */
       public boolean save(ParamBean jParams) throws JahiaException {
           logger.debug("Save Big Text..."+ getValue());
  -        ContentBigTextField contentBigTextField = (ContentBigTextField)
  +        final ContentBigTextField contentBigTextField = (ContentBigTextField)
           ContentBigTextField.getField(getID());
  -        EntrySaveRequest saveRequest = new 
EntrySaveRequest(jParams.getUser(),
  +        final EntrySaveRequest saveRequest = new 
EntrySaveRequest(jParams.getUser(),
                   this.getLanguageCode());
           if (contentBigTextField.hasActiveEntries() && getValue() != null &&
                   getValue().equals(contentBigTextField.getValue(jParams))) {
               return true;
           }
           
  -        String[] values = cleanUpHardCodedLinks(getValue(), 
jParams.getRequest());
  +        final String[] values = cleanUpHardCodedLinks(getValue(), 
jParams.getRequest(), 
  +                jParams.getLocale().toString());
           setRawValue(values[0]);
           setValue(values[1]);
           
  @@ -296,9 +266,8 @@
       }
       
       public JahiaField cloneField(int newctnid, int newPageID, int 
clonedAclID,
  -            boolean childrenCloned)
  -            throws JahiaException {
  -        JahiaField clonedField = ServicesRegistry.getInstance().
  +            boolean childrenCloned) throws JahiaException {
  +        final JahiaField clonedField = ServicesRegistry.getInstance().
                   getJahiaFieldService().
                   createJahiaField(0, this.getJahiaID(),
                   newPageID, newctnid,
  @@ -378,6 +347,13 @@
       }
       
       /**
  +     * Returns the list of 'pids' contained in this BigText
  +     */
  +    public List getInternalLinks() {
  +        return internalLinks;
  +    }
  +    
  +    /**
        * Replaces the URL marker in all the internal Jahia links and generates
        * a valid and usable URL for each marked links
        * @param content The content of the BigText
  @@ -385,10 +361,20 @@
        */
       public static String rewriteURLs(String content, ParamBean jParams) {
           logger.debug("rewriteURLs...");
  -        StringBuffer buff = new StringBuffer();
  +        
  +        if (content == null || content.length() == 0) {
  +            return "";
  +        }
  +        
  +        final StringBuffer buff = new StringBuffer();
           buff.append(jParams.getRequest().getContextPath()).
                   append(SERVLET_PATH);
           
  +        final String mode = jParams.getOperationMode();
  +        if (! ParamBean.NORMAL.equals(mode)) {
  +            buff.append("/op/").append(mode);
  +        }
  +        
           String result = StringUtils.replace(content, URL_MARKER,
                   buff.toString());
           buff.delete(0, buff.length());
  @@ -409,9 +395,11 @@
        * @return an Array of String. Position[0] contains the RawValue and 
position[1] the
        *         value that should be used when displaying the data
        */
  -    public String[] cleanUpHardCodedLinks( String content, 
HttpServletRequest req ) {
  +    public String[] cleanUpHardCodedLinks( String content, 
HttpServletRequest req, 
  +            String code) {
           logger.debug("cleanUpHardCodedLinks...");
  -        String[] result = new String[2];
  +        internalLinks.clear();
  +        final String[] result = new String[2];
           if (content.toLowerCase().indexOf("href") < 0) {
               // BigText does not contain any Jahia internal server link
               result[0] = result[1] = content;
  @@ -419,14 +407,18 @@
               return result;
           }
           
  -        StringBuffer rawResult = new StringBuffer();
  -        StringBuffer displayResult = new StringBuffer();
  +        final StringBuffer rawResult = new StringBuffer();
  +        final StringBuffer displayResult = new StringBuffer();
           
           final String delimiter = " ";
  -        StringTokenizer tokenizer = new StringTokenizer(content, delimiter);
  +        final StringTokenizer tokenizer = new StringTokenizer(content, 
delimiter);
           
           while (tokenizer.hasMoreTokens()) {   // go through each token
  -            String token = tokenizer.nextToken();
  +            final String token = tokenizer.nextToken();
  +            
  +            if ( token.trim().length() == 0) { continue; }
  +            
  +            //logger.debug("Token is: "+token);
               
               if (token.toLowerCase().startsWith("href") ) {
                   
  @@ -436,15 +428,25 @@
                   String link, rest = null;
                   if (close) { // get rid of the href=" sequence (first 6 
chars)
                       link = token.substring(6, token.indexOf('>') - 1);
  -                    if ( (token.indexOf('>') + 1) < token.length()) {
  +                    if ( (token.indexOf('>') + 1) < token.length() ) {
                           rest = token.substring(token.indexOf('>') + 1);
                       }
                   } else {
                       link = token.substring(6, token.length() - 1);
                   }
                   
  +                if (link.indexOf("://") > 0) {
  +                    // We don't want something like: http://pid/4. We want 
either
  +                    // "http://www.jahia.org/index.html"; or "pid/4"
  +                    if (link.indexOf('.') < 0) {
  +                        link = link.substring(link.indexOf("://") + 3);
  +                        
  +                    }
  +                }
  +                
                   // Check if the URL is a Jahia URL
                   if ( !(link.indexOf("pid") > -1 || hasOnlyDigits(link)) ) {
  +                    logger.debug("Not a Jahia Link: "+ link);
                       rawResult.append(token);
                       displayResult.append(token);
                       rawResult.append(delimiter);
  @@ -461,8 +463,8 @@
                       link = link.substring(8);
                       link = link.substring(link.indexOf('/'));
                       
  -                    logger.debug("Link is (no host): "+link);
                   }
  +                logger.debug("Link is: "+link);
                   
                   // All Jahia links are this stage are treated as relative 
URLs
                   if (! link.startsWith("/") && ! link.startsWith("http") ) {
  @@ -519,12 +521,24 @@
                       if (link.indexOf(URL_MARKER) < 0) {
                           rawResult.append(URL_MARKER);
                       }
  -                }
  -                
  -                // All links should have a pid
  -                if (link.indexOf("pid/") < 0) {
  -                    rawResult.append("/pid");
  -                    displayResult.append("/pid");
  +                    final Integer pageID = new 
Integer(Integer.parseInt(getPID(link)));
  +                    if (! internalLinks.contains(pageID)) {
  +                        internalLinks.add(pageID);
  +                    }
  +                    
  +                    // If no language is present in the link, put the 
current language
  +                    if (link.indexOf("lang/") < 0) {
  +                        rawResult.append("/lang/");
  +                        rawResult.append(code);
  +                        displayResult.append("/lang/");
  +                        displayResult.append(code);
  +                    }
  +                    
  +                    // All links should have a pid
  +                    if (link.indexOf("pid/") < 0) {
  +                        rawResult.append("/pid");
  +                        displayResult.append("/pid");
  +                    }
                   }
                   
                   displayResult.append(link);
  @@ -564,4 +578,65 @@
           }
           return true;
       }
  +      
  +    protected String getPID(String s) {
  +        final String value = s.substring(s.lastIndexOf('/') + 1);
  +        
  +        if (hasOnlyDigits(value)) {
  +            return value;
  +        }
  +        
  +        for (int i=0; i<value.length(); i++) {
  +            final char car = value.charAt(i);
  +            if (Character.isDigit(car)) {
  +                buff.append(car);
  +            } else {
  +                break;
  +            }
  +        }
  +        
  +        final String res = buff.toString();
  +        buff.delete(0, buff.length());
  +        return res;
  +    }
  +    
  +    /**
  +     * Sets the servlet path
  +     */
  +    protected void setServletPath(ParamBean jParams)
  +    throws JahiaException {
  +        
  +        if (SERVLET_PATH == null) { // Only perform this operation once
  +            logger.debug("Setting the SERVLET_PATH...");
  +            final String path = jParams.settings().getPathResolver().
  +                    resolvePath("/WEB-INF/web.xml");
  +            final Web_App_Xml webXmlDoc = new Web_App_Xml(path);
  +            webXmlDoc.extractDocumentData();
  +            
  +            final HashMap mappings = webXmlDoc.getServletMappings();
  +            
  +            // Default settings
  +            if (mappings.containsKey("/Jahia/*")) {
  +                SERVLET_PATH = "/Jahia";
  +                return;
  +            }
  +            
  +            Iterator ite = mappings.keySet().iterator();
  +            while (ite.hasNext()) {
  +                final String urlPattern = (String)ite.next();
  +                final String servletName = (String)mappings.get(urlPattern);
  +                
  +                if (servletName.equals("Jahia") && urlPattern.indexOf("do") 
< 0) {
  +                    SERVLET_PATH = urlPattern.substring(0,
  +                            urlPattern.lastIndexOf('/'));
  +                    return;
  +                }
  +            }
  +            
  +            throw new JahiaException("Unable to set the ServletPath",
  +                    "Unable to set the ServletPath",
  +                    JahiaException.CONFIG_ERROR,
  +                    JahiaException.CRITICAL_SEVERITY);
  +        }
  +    }
   }
  

Reply via email to