[richfaces-svn-commits] JBoss Rich Faces SVN: r2385 - in trunk/ui/insert/src/main: java/org and 5 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Tue Aug 21 16:53:37 EDT 2007


Author: alexsmirnov
Date: 2007-08-21 16:53:37 -0400 (Tue, 21 Aug 2007)
New Revision: 2385

Added:
   trunk/ui/insert/src/main/java/org/ajax4jsf/
   trunk/ui/insert/src/main/java/org/ajax4jsf/renderkit/
   trunk/ui/insert/src/main/java/org/ajax4jsf/renderkit/AbstractInsertRenderer.java
Modified:
   trunk/ui/insert/src/main/config/component/insert.xml
   trunk/ui/insert/src/main/java/org/richfaces/ui/component/UIInsert.java
   trunk/ui/insert/src/main/resources/org/richfaces/ui/renderkit/html/css/highlight.css
   trunk/ui/insert/src/main/templates/org/richfaces/ui/htmlInsert.jspx
Log:
display error information on page, instead of exception.

Modified: trunk/ui/insert/src/main/config/component/insert.xml
===================================================================
--- trunk/ui/insert/src/main/config/component/insert.xml	2007-08-21 19:37:43 UTC (rev 2384)
+++ trunk/ui/insert/src/main/config/component/insert.xml	2007-08-21 20:53:37 UTC (rev 2385)
@@ -36,5 +36,29 @@
 			<defaultvalue>"default"</defaultvalue>
 		</property>
         -->
+		<property>
+			<name>src</name>
+			<classname>java.lang.String</classname>
+			<description><![CDATA[
+			]]></description>
+		</property>
+		<property>
+			<name>highlight</name>
+			<classname>java.lang.String</classname>
+			<description><![CDATA[
+			]]></description>
+		</property>
+		<property>
+			<name>encoding</name>
+			<classname>java.lang.String</classname>
+			<description><![CDATA[
+			]]></description>
+		</property>
+		<property>
+			<name>errorContent</name>
+			<classname>java.lang.String</classname>
+			<description><![CDATA[
+			]]></description>
+		</property>
 	</component>
 </components>

Added: trunk/ui/insert/src/main/java/org/ajax4jsf/renderkit/AbstractInsertRenderer.java
===================================================================
--- trunk/ui/insert/src/main/java/org/ajax4jsf/renderkit/AbstractInsertRenderer.java	                        (rev 0)
+++ trunk/ui/insert/src/main/java/org/ajax4jsf/renderkit/AbstractInsertRenderer.java	2007-08-21 20:53:37 UTC (rev 2385)
@@ -0,0 +1,112 @@
+/**
+ * 
+ */
+package org.ajax4jsf.renderkit;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+
+import javax.faces.FacesException;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+
+import org.ajax4jsf.Messages;
+import org.ajax4jsf.renderkit.RendererUtils.HTML;
+import org.richfaces.ui.component.Highlight;
+import org.richfaces.ui.component.UIInsert;
+
+/**
+ * @author asmirnov
+ * 
+ */
+public abstract class AbstractInsertRenderer extends HeaderResourcesRendererBase {
+
+	private static final Object ERROR_MESSAGE_CLASS = "dr-insert-error";
+
+	public void renderContent(FacesContext context, UIInsert component)
+			throws IOException {
+		if (null != component.getSrc()) {
+			ExternalContext externalContext = context.getExternalContext();
+			InputStream inputStream = externalContext
+					.getResourceAsStream(component.getSrc());
+			if (null != inputStream) {
+				renderStream(context, component, inputStream);
+			} else {
+				String errorContent = component.getErrorContent();
+				if( null != errorContent && null != (inputStream = externalContext.getResourceAsStream(errorContent))){
+					// Render default content, if src not found.
+					renderStream(context, component, inputStream);
+				} else {
+					// Render error message for a not found resource.
+					ResponseWriter writer = context.getResponseWriter();
+					writer.startElement(HTML.SPAN_ELEM, component);
+					writer.writeAttribute(HTML.class_ATTRIBUTE, ERROR_MESSAGE_CLASS, null);
+					writer.write(Messages.getMessage("UI_INSERT_RESOURCE_NOT_FOUND", new Object[]{component.getClientId(context),component.getSrc()}));
+					writer.endElement(HTML.SPAN_ELEM);
+				}
+			}
+		} else {
+			throw new FacesException(
+					"Attribute 'scr' for a component <rich:insert > "
+							+ component.getClientId(context) + " must be set");
+		}
+
+	}
+
+	/**
+	 * @param context
+	 * @param component
+	 * @param inputStream
+	 * @throws UnsupportedEncodingException
+	 * @throws FacesException
+	 * @throws IOException
+	 */
+	private void renderStream(FacesContext context, UIInsert component,
+			InputStream inputStream) throws UnsupportedEncodingException,
+			FacesException, IOException {
+		ResponseWriter writer = context.getResponseWriter();
+		String encoding = component.getEncoding();
+		if (null == component.getHighlight()) {
+			InputStreamReader in;
+			if (null != encoding) {
+				in = new InputStreamReader(inputStream, encoding);
+			} else {
+				in = new InputStreamReader(inputStream);
+			}
+			char[] temp = new char[1024];
+			try {
+				int bytes;
+				while ((bytes = in.read(temp)) > 0) {
+					writer.write(temp, 0, bytes);
+				}
+			} catch (IOException e) {
+				throw new FacesException(e);
+			} finally {
+				in.close();
+			}
+		} else {
+			ByteArrayOutputStream out = new ByteArrayOutputStream();
+			try {
+				Highlight highlighter = new Highlight(component
+						.getHighlight());
+				highlighter.highlight(component.getSrc(), inputStream,
+						out, encoding, true);
+			} catch (IOException e) {
+				throw new FacesException(e);
+			} finally {
+				inputStream.close();
+			}
+			if (null != encoding) {
+				writer.write(out.toString(encoding));
+			} else {
+				writer.write(out.toString());
+			}
+		}
+	}
+
+}


Property changes on: trunk/ui/insert/src/main/java/org/ajax4jsf/renderkit/AbstractInsertRenderer.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Date Revision Author

Modified: trunk/ui/insert/src/main/java/org/richfaces/ui/component/UIInsert.java
===================================================================
--- trunk/ui/insert/src/main/java/org/richfaces/ui/component/UIInsert.java	2007-08-21 19:37:43 UTC (rev 2384)
+++ trunk/ui/insert/src/main/java/org/richfaces/ui/component/UIInsert.java	2007-08-21 20:53:37 UTC (rev 2385)
@@ -22,63 +22,7 @@
 
 	private static final String COMPONENT_FAMILY = "org.richfaces.ui.Insert";
 
-	public String getContent() {
-		String content = null;
-		if (null != getSrc()) {
-			InputStream inputStream = getFacesContext().getExternalContext()
-					.getResourceAsStream(getSrc());
-			if (null != inputStream) {
-				if (null == getHighlight()) {
-					StringBuffer buff = new StringBuffer(1024);
-					InputStreamReader in = new InputStreamReader(inputStream);
-					char[] temp = new char[1024];
-					try {
-						int bytes;
-						while ((bytes = in.read(temp)) > 0) {
-							buff.append(temp, 0, bytes);
-						}
-						;
-					} catch (IOException e) {
-						throw new FacesException(e);
-					} finally {
-						try {
-							in.close();
-						} catch (IOException e) {
-							throw new FacesException(e);
-						}
-					}
-					content = buff.toString();
-				} else {
-					ByteArrayOutputStream out = new ByteArrayOutputStream();
-					try {
-						Highlight highlighter = new Highlight(getHighlight());
-						highlighter.highlight(getSrc(), inputStream, out, null,
-								true);
-					} catch (IOException e) {
-						throw new FacesException(e);
-					} finally {
-						try {
-							inputStream.close();
-						} catch (IOException e) {
-							throw new FacesException(e);
-						}
-					}
-					content = out.toString();
-				}
-			} else {
-				throw new FacesException("Resource '" + getSrc()
-						+ "' for a component <rich:insert > "
-						+ getClientId(getFacesContext()) + " not found");
-			}
-		} else {
-			throw new FacesException(
-					"Attribute 'scr' for a component <rich:insert > "
-							+ getClientId(getFacesContext()) + " must be set");
-		}
 
-		return content;
-	}
-
 	public abstract String getSrc();
 
 	public abstract void setSrc(String src);
@@ -86,5 +30,12 @@
 	public abstract String getHighlight();
 
 	public abstract void setHighlight(String highlight);
+	
+	public abstract String getErrorContent();
+	
+	public abstract void setErrorContent(String src);
 
+	public abstract String getEncoding();
+	
+	public abstract void setEncoding(String encoding);
 }

Modified: trunk/ui/insert/src/main/resources/org/richfaces/ui/renderkit/html/css/highlight.css
===================================================================
--- trunk/ui/insert/src/main/resources/org/richfaces/ui/renderkit/html/css/highlight.css	2007-08-21 19:37:43 UTC (rev 2384)
+++ trunk/ui/insert/src/main/resources/org/richfaces/ui/renderkit/html/css/highlight.css	2007-08-21 20:53:37 UTC (rev 2385)
@@ -132,4 +132,9 @@
 .cpp_preproc { 
 
 	color: purple;
+}
+
+.dr-insert-error {
+	color: red; 
+	font-weight: bold;
 }
\ No newline at end of file

Modified: trunk/ui/insert/src/main/templates/org/richfaces/ui/htmlInsert.jspx
===================================================================
--- trunk/ui/insert/src/main/templates/org/richfaces/ui/htmlInsert.jspx	2007-08-21 19:37:43 UTC (rev 2384)
+++ trunk/ui/insert/src/main/templates/org/richfaces/ui/htmlInsert.jspx	2007-08-21 20:53:37 UTC (rev 2385)
@@ -6,7 +6,7 @@
 	xmlns:u=" http://ajax4jsf.org/cdk/u"
 	xmlns:x=" http://ajax4jsf.org/cdk/x"
 	class="org.richfaces.ui.renderkit.html.InsertRenderer"
-	baseclass="org.ajax4jsf.renderkit.AjaxComponentRendererBase"
+	baseclass="org.ajax4jsf.renderkit.AbstractInsertRenderer"
 	component="org.richfaces.ui.component.UIInsert" 
 	>
 	<h:styles>css/highlight.css</h:styles>  
@@ -14,8 +14,6 @@
 	<div id="#{clientId}"
 		x:passThruWithExclusions="value,name,type,id"
 		>
-		<jsp:scriptlet>
-		  writer.write(component.getContent());
-		</jsp:scriptlet>
+		<f:call name="renderContent"/>
 	</div>
 </f:root>
\ No newline at end of file




More information about the richfaces-svn-commits mailing list