You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ma...@apache.org on 2005/11/25 11:19:33 UTC

svn commit: r348925 - in /myfaces/tomahawk/trunk: src/java/org/apache/myfaces/custom/stylesheet/ tld/

Author: matzew
Date: Fri Nov 25 02:19:22 2005
New Revision: 348925

URL: http://svn.apache.org/viewcvs?rev=348925&view=rev
Log:
finally, applyed patch from Stephan Strittmatter. Thanks Stephan!

Modified:
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/Stylesheet.java
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/StylesheetRenderer.java
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/StylesheetTag.java
    myfaces/tomahawk/trunk/tld/tomahawk.tld

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/Stylesheet.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/Stylesheet.java?rev=348925&r1=348924&r2=348925&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/Stylesheet.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/Stylesheet.java Fri Nov 25 02:19:22 2005
@@ -36,6 +36,8 @@
 	private static final Log log = LogFactory.getLog(Stylesheet.class);
 
     private String _path = null;
+    private Boolean _inline = null;
+    private String _media = null;
 
 
     // ------------------------------------------------------------ Constructors
@@ -63,20 +65,54 @@
     public void setPath(String path) {
         this._path = path;
     }
+    
+    /**
+     * @return true if the styles are inlined to the jsp file
+     */
+    public boolean isInline() {
+        if (this._inline != null)
+          return this._inline.booleanValue();
+            ValueBinding vb = getValueBinding("inline");
+            return ((vb != null) ? (Boolean) vb.getValue(getFacesContext())
+                : Boolean.FALSE).booleanValue();
+      }
+
+    /**
+     * @param inline if true, the css-file is inlined to the jsp file. Default is false.
+     */
+    public void setInline(boolean inline) {
+        this._inline = Boolean.valueOf(inline);
+    }
+    
 
+    public String getMedia() {
+    	if (this._media != null)
+    		return this._media;
+    	ValueBinding vb = getValueBinding("media");
+    		return ((vb != null) ? (String) vb.getValue(getFacesContext()) : null);
+    	}
+    
+    public void setMedia(String media) {
+    	this._media = media;  
+    }
+    
     public void restoreState(FacesContext context, Object state) {
 
         Object values[] = (Object[]) state;
         super.restoreState(context, values[0]);
         _path = (String) values[1];
+        _inline = (Boolean) values[2];
+        _media = (String) values[3];
 
     }
 
     public Object saveState(FacesContext context) {
 
-        Object values[] = new Object[2];
+        Object values[] = new Object[4];
         values[0] = super.saveState(context);
         values[1] = _path;
+        values[2] = _inline;
+        values[3] = _media;
         return values;
 
     }

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/StylesheetRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/StylesheetRenderer.java?rev=348925&r1=348924&r2=348925&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/StylesheetRenderer.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/StylesheetRenderer.java Fri Nov 25 02:19:22 2005
@@ -15,6 +15,9 @@
  */
 package org.apache.myfaces.custom.stylesheet;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 
 import javax.faces.component.UIComponent;
@@ -33,8 +36,8 @@
 
 public class StylesheetRenderer extends HtmlRenderer {
 
-	 private static final Log log = LogFactory.getLog(StylesheetRenderer.class);
-
+	 private static final Log LOG = LogFactory.getLog(StylesheetRenderer.class);
+      
 	 public void encodeEnd(FacesContext context, UIComponent component)
         throws IOException {
 
@@ -42,15 +45,81 @@
             throw new NullPointerException();
         }
         Stylesheet stylesheet = (Stylesheet) component;
-
         ResponseWriter writer = context.getResponseWriter();
-        writer.startElement("link", component);
-        writer.writeAttribute("rel", "stylesheet", null);
-        writer.writeAttribute("type", "text/css", null);
-        writer.writeURIAttribute
-            ("href",
-             context.getExternalContext().getRequestContextPath()+stylesheet.getPath(),
-			 "path");
-        writer.endElement("link");
+        
+        if(stylesheet.isInline())
+        {
+          //include as inline css
+          writer.startElement("style", component);
+          writer.writeAttribute("type", "text/css", null);
+          if(stylesheet.getMedia() != null)
+          {
+            writer.writeAttribute("media", stylesheet.getMedia(), null);
+          }          
+          //writer.writeText("<!--\n", null);
+          writer.writeText(loadFile(context.getExternalContext().getRequestContextPath(), stylesheet.getPath()), null);
+          //writer.writeText("\n-->", null);
+          writer.endElement("style");  
+        }
+        else
+        {
+          //refere as link-element
+          writer.startElement("link", component);
+          writer.writeAttribute("rel", "stylesheet", null);
+          writer.writeAttribute("type", "text/css", null);
+          if(stylesheet.getMedia() != null)
+          {
+            writer.writeAttribute("media", stylesheet.getMedia(), null);
+          }
+          writer.writeURIAttribute
+              ("href",
+               context.getExternalContext().getRequestContextPath()+stylesheet.getPath(),
+             "path");
+          writer.endElement("link");          
+        }
+        
+
+    }
+
+  private Object loadFile(String ctxPath, String file)
+  {
+    
+    String href = ctxPath+file;
+    LOG.debug("loadFile: " + href);
+    
+    File cssFile = new File(href);
+    String content;
+    
+
+
+    if (cssFile.canRead())
+    {
+      FileInputStream in;
+      try {
+        in = new FileInputStream(cssFile);
+        byte[] fileBuffer = new byte[(int) cssFile.length()];
+        in.read(fileBuffer);
+        in.close();
+        content = new String(fileBuffer);
+      }
+      catch (FileNotFoundException e) {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
+        content=null;
+      }
+      catch (IOException e) {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
+        content=null;
+      }
+
+    }
+    else
+    {
+      LOG.error("File not readable: " + href);
+      content=null;
     }
+    
+    return content;
+  }
 }

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/StylesheetTag.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/StylesheetTag.java?rev=348925&r1=348924&r2=348925&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/StylesheetTag.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/stylesheet/StylesheetTag.java Fri Nov 25 02:19:22 2005
@@ -27,6 +27,8 @@
 
 
     private String _path = null;
+    private boolean _inline = false;
+    
 	// User Role support
 	private String _enabledOnUserRole;
 	private String _visibleOnUserRole;
@@ -76,4 +78,16 @@
 	public void setVisibleOnUserRole(String string) {
 		_visibleOnUserRole = string;
 	}
+
+  
+  public boolean isInline()
+  {
+    return _inline;
+  }
+
+  
+  public void setInline(boolean inline)
+  {
+    this._inline = inline;
+  }
 }

Modified: myfaces/tomahawk/trunk/tld/tomahawk.tld
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/tld/tomahawk.tld?rev=348925&r1=348924&r2=348925&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/tld/tomahawk.tld (original)
+++ myfaces/tomahawk/trunk/tld/tomahawk.tld Fri Nov 25 02:19:22 2005
@@ -1923,6 +1923,43 @@
 				URL for CSS-file.
 			</description>
 		</attribute>
+		<attribute>
+			<name>inline</name>
+			<required>false</required>
+			<rtexprvalue>false</rtexprvalue>
+			<description>
+				Inline the stylesheet file content as &lt;style&gt;-tag in contrast
+                to refere it as &lt;link&gt;-tag.
+			</description>
+		</attribute>
+		<attribute>
+			<name>media</name>
+			<required>false</required>
+			<rtexprvalue>false</rtexprvalue>
+			<description>
+				Define the target media of the styles:
+				screen
+				    Intended for non-paged computer screens.
+				tty
+				    Intended for media using a fixed-pitch character grid, such as teletypes, terminals, or portable devices with limited display capabilities.
+				tv
+				    Intended for television-type devices (low resolution, color, limited scrollability).
+				projection
+				    Intended for projectors.
+				handheld
+				    Intended for handheld devices (small screen, monochrome, bitmapped graphics, limited bandwidth).
+				print
+				    Intended for paged, opaque material and for documents viewed on screen in print preview mode.
+				braille
+				    Intended for braille tactile feedback devices.
+				aural
+				    Intended for speech synthesizers.
+				all
+				    Suitable for all devices. 
+                Could be a comma separated list.
+                See also http://www.w3.org/TR/REC-html40/types.html#type-media-descriptors
+			</description>
+		</attribute>
 	</tag>
 
 	<!-- div -->