incubator-connectors-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwri...@apache.org
Subject svn commit: r1224641 [8/8] - in /incubator/lcf/branches/CONNECTORS-335: ./ connectors/activedirectory/connector/src/main/java/org/apache/manifoldcf/authorities/authorities/activedirectory/ connectors/alfresco/ connectors/alfresco/connector/src/main/jav...
Date Sun, 25 Dec 2011 20:11:01 GMT
Added: incubator/lcf/branches/CONNECTORS-335/framework/ui-core/src/main/java/org/apache/manifoldcf/ui/i18n/Messages.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-335/framework/ui-core/src/main/java/org/apache/manifoldcf/ui/i18n/Messages.java?rev=1224641&view=auto
==============================================================================
--- incubator/lcf/branches/CONNECTORS-335/framework/ui-core/src/main/java/org/apache/manifoldcf/ui/i18n/Messages.java
(added)
+++ incubator/lcf/branches/CONNECTORS-335/framework/ui-core/src/main/java/org/apache/manifoldcf/ui/i18n/Messages.java
Sun Dec 25 20:10:58 2011
@@ -0,0 +1,155 @@
+/* $Id$ */
+
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.manifoldcf.ui.i18n;
+
+import java.util.Locale;
+import java.util.Map;
+import java.util.Iterator;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.BufferedReader;
+import java.io.IOException;
+
+import org.apache.manifoldcf.core.interfaces.ManifoldCFException;
+import org.apache.manifoldcf.core.interfaces.IHTTPOutput;
+
+import org.apache.manifoldcf.ui.util.Encoder;
+
+import org.apache.commons.lang.StringUtils;
+
+public class Messages extends org.apache.manifoldcf.core.i18n.Messages
+{
+  /** Constructor - do no instantiate
+  */
+  protected Messages()
+  {
+  }
+  
+  /** Write a resource to HTTP output, specifying what to substitute, and more importantly,
how.
+  * ${PARAM_NAME} will be substituted directly with the value.
+  * ${PARAM_NAME:A} will be substituted with HTML attribute-escaped value.
+  * ${PARAM_NAME:B} will be substituted with HTML body-escaped value.
+  * ${PARAM_NAME:AJ} will be substituted with HTML attribute + Javascript escaped value.
+  * ${PARAM_NAME:BJ} will be substituted with HTML body + Javascript escaped value.
+  */
+  public static void outputResource(IHTTPOutput output, Class classInstance, String pathName,
+    Locale locale, String resourceKey, Map<String,String> substitutionParameters, boolean
mapToUpperCase)
+    throws ManifoldCFException
+  {
+    try
+    {
+      InputStream is = getResourceAsStream(classInstance,pathName,locale,resourceKey);
+      try
+      {
+        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+        String line;
+        while ((line = br.readLine()) != null)
+        {
+          if (substitutionParameters != null)
+          {
+            Iterator<String> i = substitutionParameters.keySet().iterator();
+            boolean parsedLine = false;
+            while(i.hasNext())
+            {
+              String key = i.next();
+              String value = substitutionParameters.get(key);
+              if (mapToUpperCase)
+                key = key.toUpperCase();
+              if (value == null)
+                value = "";
+              //System.out.println("Processed key = '"+key+"', processed value = '"+value+"'");
+              // We replace 4x, with 4 different replacement strings
+              line = doReplace(line,"${"+key+"}",value);
+              line = doReplace(line,"${"+key+":A}",Encoder.attributeEscape(value));
+              line = doReplace(line,"${"+key+":B}",Encoder.bodyEscape(value));
+              line = doReplace(line,"${"+key+":AJ}",Encoder.attributeJavascriptEscape(value));
+              line = doReplace(line,"${"+key+":BJ}",Encoder.bodyJavascriptEscape(value));
+            }
+          }
+          output.println(line);
+        }
+      }
+      finally
+      {
+        is.close();
+      }
+    }
+    catch (IOException e)
+    {
+      throw new ManifoldCFException(e.getMessage(),e);
+    }
+  }
+
+  private static String doReplace(String line, String key, String value)
+  {
+    if (line.indexOf(key) == -1)
+    {
+      return line;
+    }
+    StringBuilder sb = new StringBuilder();
+    int index = 0;
+    while (true)
+    {
+      int newIndex = line.indexOf(key,index);
+      if (newIndex == -1)
+      {
+        sb.append(line.substring(index));
+        break;
+      }
+      sb.append(line.substring(index,newIndex)).append(value);
+      index = newIndex + key.length();
+    }
+    return sb.toString();
+  }
+  
+  /** Obtain a string given a classloader, bundle, locale, message key, and arguments, and
escape it for HTML body context.
+  */
+  public static String getBodyString(ClassLoader classLoader, String bundleName, Locale locale,
+    String messageKey, Object[] args)
+  {
+    return Encoder.bodyEscape(getString(classLoader,bundleName,locale,messageKey,args));
+  }
+  
+  /** Obtain a string given a classloader, bundle, locale, message key, and arguments, and
escape it for HTML attribute context.
+  */
+  public static String getAttributeString(ClassLoader classLoader, String bundleName, Locale
locale,
+    String messageKey, Object[] args)
+  {
+    return Encoder.attributeEscape(getString(classLoader,bundleName,locale,messageKey,args));
+  }
+
+  /** Obtain a string given a classloader, bundle, locale, message key, and arguments, and
escape it for HTML attribute/javascript context.
+  */
+  public static String getAttributeJavascriptString(ClassLoader classLoader, String bundleName,
Locale locale,
+    String messageKey, Object[] args)
+  {
+    return Encoder.attributeJavascriptEscape(getString(classLoader,bundleName,locale,messageKey,args));
+  }
+
+  /** Obtain a string given a classloader, bundle, locale, message key, and arguments, and
escape it for HTML body/javascript context.
+  */
+  public static String getBodyJavascriptString(ClassLoader classLoader, String bundleName,
Locale locale,
+    String messageKey, Object[] args)
+  {
+    return Encoder.bodyJavascriptEscape(getString(classLoader,bundleName,locale,messageKey,args));
+  }
+
+}
+

Propchange: incubator/lcf/branches/CONNECTORS-335/framework/ui-core/src/main/java/org/apache/manifoldcf/ui/i18n/Messages.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/lcf/branches/CONNECTORS-335/framework/ui-core/src/main/java/org/apache/manifoldcf/ui/i18n/Messages.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: incubator/lcf/branches/CONNECTORS-335/framework/ui-core/src/main/java/org/apache/manifoldcf/ui/util/Encoder.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-335/framework/ui-core/src/main/java/org/apache/manifoldcf/ui/util/Encoder.java?rev=1224641&r1=1224640&r2=1224641&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-335/framework/ui-core/src/main/java/org/apache/manifoldcf/ui/util/Encoder.java
(original)
+++ incubator/lcf/branches/CONNECTORS-335/framework/ui-core/src/main/java/org/apache/manifoldcf/ui/util/Encoder.java
Sun Dec 25 20:10:58 2011
@@ -46,6 +46,28 @@ public class Encoder
     return attributeEscape(rval.toString());
   }
 
+  /** Escape a string that's in an html body (script) area and thence inside javascript.
+  *@param value is the input.
+  *@return the escaped string.
+  */
+  public static String bodyJavascriptEscape(String value)
+  {
+    StringBuilder rval = new StringBuilder();
+    int i = 0;
+    while (i < value.length())
+    {
+      char x = value.charAt(i++);
+      // First level of encoding: javascript string
+      if (x == '\\' || x == '"' || x == '\'')
+      {
+        rval.append("\\").append(x);
+      }
+      else
+        rval.append(x);
+    }
+    return bodyEscape(rval.toString());
+  }
+
   /** Escape a string that's inside an html attribute.
   *@param value is the input.
   *@return the escaped string.



Mime
View raw message