[richfaces-svn-commits] JBoss Rich Faces SVN: r12059 - in trunk/ui/editor/src/main/java/org/richfaces/renderkit: resources and 1 other directory.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Tue Dec 30 05:47:02 EST 2008


Author: alevkovsky
Date: 2008-12-30 05:47:02 -0500 (Tue, 30 Dec 2008)
New Revision: 12059

Modified:
   trunk/ui/editor/src/main/java/org/richfaces/renderkit/EditorRendererBase.java
   trunk/ui/editor/src/main/java/org/richfaces/renderkit/resources/EditorHTMLRenderer.java
Log:
Refactoring after code review

Modified: trunk/ui/editor/src/main/java/org/richfaces/renderkit/EditorRendererBase.java
===================================================================
--- trunk/ui/editor/src/main/java/org/richfaces/renderkit/EditorRendererBase.java	2008-12-30 10:39:32 UTC (rev 12058)
+++ trunk/ui/editor/src/main/java/org/richfaces/renderkit/EditorRendererBase.java	2008-12-30 10:47:02 UTC (rev 12059)
@@ -22,6 +22,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -37,6 +38,7 @@
 
 import org.ajax4jsf.javascript.ScriptUtils;
 import org.ajax4jsf.resource.InternetResource;
+import org.ajax4jsf.resource.util.URLToStreamHelper;
 import org.ajax4jsf.util.InputUtils;
 import org.richfaces.component.UIEditor;
 
@@ -74,20 +76,6 @@
 		return InputUtils.getConvertedValue(context, component, submittedValue);
 	}
 
-	/**
-	 * Method to get converted to String model value for component
-	 * 
-	 * @param context - faces context instance
-	 * @param component - component for which method is applied 
-	 * @param value - component value
-	 * @return converted to String model value
-	 */
-	//TODO  nick - do we really need this method?
-	protected String getConvertedStringValue(FacesContext context,
-			UIEditor component, Object value) {
-		return InputUtils.getConvertedStringValue(context, component, value);
-	}
-
 	/* (non-Javadoc)
 	 * @see org.richfaces.renderkit.InputRendererBase#doDecode(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
 	 */
@@ -115,8 +103,8 @@
 			UIEditor component) {
 		String fieldValue = (String) component.getSubmittedValue();
 		if (fieldValue == null) {
-			fieldValue = getConvertedStringValue(context, component, component
-					.getValue());
+			fieldValue = InputUtils.getConvertedStringValue(context, component,
+					component.getValue());
 		}
 		return fieldValue;
 	}
@@ -152,11 +140,6 @@
 		InternetResource resource = getResource(resourceName);
 		String resourceUri = resource.getUri(context, null);
 		String suffix = resourceUri.substring(resourceUri.indexOf(resourceName) + resourceName.length());
-		
-		//TODO nick - I doubt suffix can be null
-		if(suffix == null){
-			suffix = "";
-		}
 		return suffix;
 	}
 
@@ -173,31 +156,9 @@
 		ResponseWriter writer = context.getResponseWriter();
 		writer.writeText("var tinyMceParams = ", null);
 
-		String configName = component.getConfiguration();
-		if (configName != null && configName.length() > 0) {
-			Properties parameters = new Properties();
-			try {
-				ClassLoader loader = Thread.currentThread()
-						.getContextClassLoader();
-				
-				//TODO nick - use org.ajax4jsf.resource.util.URLToStreamHelper.urlToStream(URL)
-				InputStream is = loader.getResourceAsStream(configName
-						+ ".properties");
-				if (is == null) {
-					throw new FacesException(
-							"Editor configuration properties file with name '"
-									+ configName
-									+ "' was not found in class path");
-				}
-				
-				//TODO nick - streams should be closed, in finally block
-				parameters.load(is);
-				writer.writeText(this.convertProperties(parameters), null);
-				writer.writeText(";\n", null);
-			} catch (IOException e) {
-				throw new FacesException(e);
-			}
-
+		String parametersConfigName = component.getConfiguration();
+		if (parametersConfigName != null && parametersConfigName.length() > 0) {
+			loadPropertiesAndWrite(context, parametersConfigName, writer, false);
 		} else {
 			writer.writeText("{};\n", null);
 		}
@@ -211,34 +172,59 @@
 	 * @param component - Editor component instance
 	 * @throws IOException
 	 */
-	//TODO nick - merge with writeEditorConfigurationParameters() method
+	//XXX nick - merge with writeEditorConfigurationParameters() method
 	public void writeEditorCustomPluginsParameters(FacesContext context,
 			UIEditor component) throws IOException {
 		ResponseWriter writer = context.getResponseWriter();
 
-		String configName = component.getCustomPlugins();
-		if (configName != null && configName.length() > 0) {
-			Properties parameters = new Properties();
+		String customPluginsConfigName = component.getCustomPlugins();
+		if (customPluginsConfigName != null && customPluginsConfigName.length() > 0) {
+			loadPropertiesAndWrite(context, customPluginsConfigName, writer, true);
+		}
+	}
+	
+	/**
+	 * Method to load configuration parameters from property file and write them
+	 * @param context
+	 * @param configName
+	 * @param writer
+	 * @param cutomPlugins
+	 */
+	private void loadPropertiesAndWrite(FacesContext context,
+			String configName, ResponseWriter writer, boolean cutomPlugins) {
+		Properties parameters = new Properties();
+		try {
+			ClassLoader loader = Thread.currentThread().getContextClassLoader();
+
+			// XXX nick - use org.ajax4jsf.resource.util.URLToStreamHelper.urlToStream(URL)
+			URL url = loader.getResource(configName + ".properties");
+			InputStream is = URLToStreamHelper.urlToStream(url);
+			if (is == null) {
+				throw new FacesException(
+						"Editor configuration properties file with name '"
+								+ configName + "' was not found in class path");
+			}
+
+			// XXX nick - streams should be closed, in finally block
 			try {
-				ClassLoader loader = Thread.currentThread()
-						.getContextClassLoader();
-				InputStream is = loader.getResourceAsStream(configName
-						+ ".properties");
-				if (is == null) {
-					throw new FacesException(
-							"Editor configuration properties file with name '"
-									+ configName
-									+ "' was not found in class path");
-				}
 				parameters.load(is);
-				writer.writeText("\n", null);
-				writer.writeText(this.getCustomPluginsCode(context, parameters), null);
-				writer.writeText("\n", null);
-			} catch (IOException e) {
+			} catch (Exception e) {
 				throw new FacesException(e);
+			} finally {
+				is.close();
 			}
-
+			writer.writeText("\n", null);
+			if (cutomPlugins) {
+				writer.writeText(
+						this.getCustomPluginsCode(context, parameters), null);
+			} else {
+				writer.writeText(this.convertProperties(parameters), null);
+			}
+			writer.writeText(";\n", null);
+		} catch (IOException e) {
+			throw new FacesException(e);
 		}
+
 	}
 
 	/**
@@ -300,87 +286,93 @@
 			UIEditor component) throws IOException {
 		ResponseWriter writer = context.getResponseWriter();
 
-		if (component.getTheme() != null && component.getTheme().length() > 0) {
+		String theme = component.getTheme();
+		String language = component.getLanguage();
+		String plugins = component.getPlugins();
+		String oninit = component.getOninit();
+		String onsave = component.getOnsave();
+		String onchange = component.getOnchange();
+		String onsetup = component.getOnsetup();
+		String dialogType = component.getDialogType();
+		String skin = component.getSkin();
+		Integer width = component.getWidth();
+		Integer height = component.getHeight();
+
+		if (theme != null && theme.length() > 0) {
 			writer.writeText("tinyMceParams.theme = "
-					+ ScriptUtils.toScript(component.getTheme()) + ";\n", null);
+					+ ScriptUtils.toScript(theme) + ";\n", null);
 		}
-		if (component.getLanguage() != null
-				&& component.getLanguage().length() > 0) {
+		if (language != null && language.length() > 0) {
 			writer.writeText("tinyMceParams.language = "
-					+ ScriptUtils.toScript(component.getLanguage()) + ";\n",
-					null);
+					+ ScriptUtils.toScript(language) + ";\n", null);
 		}
 		writer.writeText("tinyMceParams.auto_resize = "
-				+ ScriptUtils.toScript(component.isAutoResize()) + ";\n",
-				null);
+				+ ScriptUtils.toScript(component.isAutoResize()) + ";\n", null);
 
 		writer.writeText("tinyMceParams.readonly = "
-				+ ScriptUtils.toScript(component.isReadonly()) + ";\n",
-				null);
-		if (component.getPlugins() != null
-				&& component.getPlugins().length() > 0) {
+				+ ScriptUtils.toScript(component.isReadonly()) + ";\n", null);
+
+		if (plugins != null && plugins.length() > 0) {
 			writer.writeText("tinyMceParams.plugins = "
-					+ ScriptUtils.toScript(component.getPlugins()) + ";\n",
-					null);
+					+ ScriptUtils.toScript(plugins) + ";\n", null);
 		}
-		if (component.getWidth() != null) {
+		if (width != null) {
 			writer.writeText("tinyMceParams.width = "
-					+ ScriptUtils.toScript(component.getWidth()) + ";\n", null);
+					+ ScriptUtils.toScript(width) + ";\n", null);
 		}
-		if (component.getHeight() != null) {
-			writer
-					.writeText("tinyMceParams.height = "
-							+ ScriptUtils.toScript(component.getHeight())
-							+ ";\n", null);
+		if (height != null) {
+			writer.writeText("tinyMceParams.height = "
+					+ ScriptUtils.toScript(height) + ";\n", null);
 		}
-		
-		//TODO nick - use local variables
-		if (component.getOninit() != null && component.getOninit().length() > 0) {
+
+		// XXX nick - use local variables
+		if (oninit != null && oninit.length() > 0) {
 			writer.writeText("tinyMceParams.oninit = function (event) {\n"
-					+ component.getOninit() + "\n" + "};\n", null);
+					+ oninit + "\n" + "};\n", null);
 		}
-		if (component.getOnsave() != null && component.getOnsave().length() > 0) {
-			writer.writeText(
-					"tinyMceParams.save_callback = function (event, element_id, html, body) {\n		return "
-							+ component.getOnsave() + "\n" + "};\n", null);
+		if (onsave != null && onsave.length() > 0) {
+			writer
+					.writeText(
+							"tinyMceParams.save_callback = function (event, element_id, html, body) {\n		return "
+									+ onsave + "\n" + "};\n", null);
 		}
-		if (component.getOnchange() != null
-				&& component.getOnchange().length() > 0) {
+		if (onchange != null && onchange.length() > 0) {
 			writer.writeText(
 					"tinyMceParams.onchange_callback = function (event, inst) {\n"
-							+ component.getOnchange() + "\n" + "};\n", null);
+							+ onchange + "\n" + "};\n", null);
 		}
-		if (component.getOnsetup() != null
-				&& component.getOnsetup().length() > 0) {
+		if (onsetup != null && onsetup.length() > 0) {
 			writer.writeText("tinyMceParams.setup = function (event, ed) {\n"
-					+ component.getOnsetup() + "\n" + "};\n", null);
+					+ onsetup + "\n" + "};\n", null);
 
 		}
-		if (component.getDialogType() != null
-				&& component.getDialogType().length() > 0) {
+		if (dialogType != null && dialogType.length() > 0) {
 			writer.writeText("tinyMceParams.dialog_type = "
-					+ ScriptUtils.toScript(component.getDialogType()) + ";\n",
-					null);
+					+ ScriptUtils.toScript(dialogType) + ";\n", null);
 		}
-		if (component.getSkin() != null && component.getSkin().length() > 0) {
+		if (skin != null && skin.length() > 0) {
 			writer.writeText("tinyMceParams.skin = "
-					+ ScriptUtils.toScript(component.getSkin()) + ";\n", null);
+					+ ScriptUtils.toScript(skin) + ";\n", null);
 		} else {
 			writer.writeText("if(!tinyMceParams.skin){\n", null);
 			writer.writeText("	tinyMceParams.skin = 'richfaces';\n", null);
 			writer.writeText("}\n", null);
 		}
-		
-		writer.writeText("if(tinyMceParams.strict_loading_mode == null){\n", null);
+
+		writer.writeText("if(tinyMceParams.strict_loading_mode == null){\n",
+				null);
 		writer.writeText("	tinyMceParams.strict_loading_mode = true;\n", null);
 		writer.writeText("}\n", null);
 	}
 
 	/**
-	 * Method to write tinyMCE configuration script parameters which was defined as <f:param> children for Editor
+	 * Method to write tinyMCE configuration script parameters which was defined
+	 * as <f:param> children for Editor
 	 * 
-	 * @param context - faces context instance
-	 * @param component - Editor component instance
+	 * @param context -
+	 *            faces context instance
+	 * @param component -
+	 *            Editor component instance
 	 * @throws IOException
 	 */
 	public void writeEditorParameters(FacesContext context,
@@ -415,10 +407,8 @@
 	 * @return true if needed or false if only target textarea should be rendered 
 	 */
 	public boolean shouldRenderTinyMCE(UIEditor component) {
-		//TODO nick - use TINY_MCE_DISABLED_MODE.equalsIgnoreCase(component.getViewMode())
-		if (component.getViewMode() != null
-				&& component.getViewMode().equalsIgnoreCase(
-						TINY_MCE_DISABLED_MODE)) {
+		//XXX nick - use TINY_MCE_DISABLED_MODE.equalsIgnoreCase(component.getViewMode())
+		if (TINY_MCE_DISABLED_MODE.equalsIgnoreCase(component.getViewMode())) {
 			return false;
 		} else {
 			return true;

Modified: trunk/ui/editor/src/main/java/org/richfaces/renderkit/resources/EditorHTMLRenderer.java
===================================================================
--- trunk/ui/editor/src/main/java/org/richfaces/renderkit/resources/EditorHTMLRenderer.java	2008-12-30 10:39:32 UTC (rev 12058)
+++ trunk/ui/editor/src/main/java/org/richfaces/renderkit/resources/EditorHTMLRenderer.java	2008-12-30 10:47:02 UTC (rev 12059)
@@ -21,12 +21,10 @@
 package org.richfaces.renderkit.resources;
 
 import java.io.BufferedReader;
-import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
-import java.io.OutputStreamWriter;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -55,6 +53,11 @@
 	/** Regular expression pattern for finding elements for replacing and correcting */
 	private final static Pattern REGEXP = Pattern.compile("\\<(script|link|img|iframe)\\s+.*(?:src|href)\\s*=\\s*[\"']([^'\"]+)\\.([^\\'\"]+)[\"'][^>]*\\>", Pattern.CASE_INSENSITIVE);
 	
+	/** Script resource suffix due to servlet or filter mappings */
+	private String scriptSuffix;
+	/** XCSS resource suffix due to servlet or filter mappings */
+	private String xcssSuffix;
+
 	/* (non-Javadoc)
 	 * @see org.ajax4jsf.resource.BaseResourceRenderer#getCommonAttrs()
 	 */
@@ -118,12 +121,11 @@
 		int total = 0;
 		BufferedReader br = new BufferedReader(new InputStreamReader(in));
 		
-		//TODO nick - why we need BufferedWriter?
-		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
+		//XXX nick - why we need BufferedWriter?
 		
-		//TODO nick - cannot be changed in runtime, cache in private field
-		String scriptSuffix = getSriptMappingSuffix();
-		String cssSuffix = getCssMappingSuffix();
+		//XXX nick - cannot be changed in runtime, cache in private field
+		String scriptSuffix = getScriptSuffix();
+		String cssSuffix = getXcssSuffix();
 		
 		Pattern pattern = REGEXP;
 		
@@ -155,16 +157,13 @@
 						line = line.replace(path  + "." + extension, path  + "." + (extension.charAt(0)=='c' ? "xcss" : "XCSS") + cssSuffix);
 					}
 				}
-				bw.write(line);
-				bw.newLine();
+				line += "\n";
+				out.write(line.getBytes());
 				total += line.getBytes().length;
 			}
 		} finally {
-			//TODO nick - close always flushes, no need to do that explicitly
-			bw.flush();
+			//XXX nick - close always flushes, no need to do that explicitly
 			br.close();
-			bw.close();
-			in.close();
 			out.close();
 		}
 		return total;
@@ -213,5 +212,29 @@
 	public boolean requireFacesContext() {
 		return true;
 	}
+	
+	/**
+	 * Getter for scriptSuffix
+	 * 
+	 * @return the scriptSuffix
+	 */
+	public String getScriptSuffix() {
+		if(scriptSuffix == null){
+			scriptSuffix = getSriptMappingSuffix();
+		}
+		return scriptSuffix;
+	}
 
+	/**
+	 * Getter for xcssSuffix
+	 * 
+	 * @return the xcssSuffix
+	 */
+	public String getXcssSuffix() {
+		if(xcssSuffix == null){
+			xcssSuffix = getCssMappingSuffix();
+		}
+		return xcssSuffix;
+	}
+
 }




More information about the richfaces-svn-commits mailing list