001 package biz.wedoit4u;
002
003 import java.io.FileNotFoundException;
004 import java.io.FileOutputStream;
005 import java.io.PrintWriter;
006 import java.util.Collection;
007 import java.util.Iterator;
008
009 import javax.naming.NamingException;
010
011 import biz.wedoit4u.databeans.CustomerStyleBean;
012 import biz.wedoit4u.databeans.StyleClassBean;
013
014 /**
015 * Generate a full CSS string from the values in the
016 * <code>style_attributes</code> table for a specified customer. The
017 * CSS values may be retrieved as a regular Java String, or be written
018 * out to a file for use in a web server.
019 *
020 * <p>Copyright 2003, Rakesh Vidyadharan and wedoit4u.biz</p>
021 *
022 * @author Rakesh Vidyadharan 16<sup><small>th</small></sup> November 2003
023 *
024 * @version $Id: CSSGenerator.java,v 1.3 2004/05/26 11:42:30 rakesh Exp $
025 */
026 public final class CSSGenerator extends Object
027 {
028 /**
029 * The customer for whom the CSS contents are being generated. Maps
030 * to the <code>customer_id</code> value.
031 */
032 private int customerId = 0;
033
034 /**
035 * String that holds the entire CSS contents for the customer
036 * identified by {@link #customerId}.
037 */
038 private String cssContents = "";
039
040 /**
041 * Create a new instance of the class for the specified customer.
042 * Initialises the {@link #customerId} instance variable, and then
043 * fetches all the style attributes associated with that customer.
044 * Populates the {@link #cssContents} value using {@link
045 * #loadCSSContents}.
046 *
047 * @param customerId - The <code>customer_id</code> value to use
048 * to generate the CSS contents.
049 * @throws CMAException - If errors are encountered while fetching the
050 * associated style classes for the customer.
051 */
052 public CSSGenerator( int customerId ) throws CMAException
053 {
054 this.customerId = customerId;
055 loadCSSContents();
056 }
057
058 /**
059 * Fetch all the style attributes associated with the customer, and
060 * populate the {@link #cssContents} instance variable.
061 *
062 * @throws CMAException - If errors are encountered while fetching the
063 * associated style classes for the customer.
064 */
065 private void loadCSSContents() throws CMAException
066 {
067 StringBuffer contents = new StringBuffer( 1024 );
068 Collection collection = CustomerStyleBean.findByCustomer( customerId );
069 for ( Iterator iterator = collection.iterator(); iterator.hasNext(); )
070 {
071 CustomerStyleBean sab = (CustomerStyleBean) iterator.next();
072 String className = StyleClassBean.getInstance( sab.getStyleClassId() ).className;
073 contents.append( className ).append( "\n{\n" );
074
075 if ( sab.color != null && ! sab.color.equals( "" ) )
076 {
077 contents.append( "color: " ).append( sab.color ).append( ";\n" );
078 }
079 if ( sab.fontFamily != null && ! sab.fontFamily.equals( "" ) )
080 {
081 contents.append( "font-family: " ).append( sab.fontFamily ).append( ";\n" );
082 }
083 if ( sab.fontWeight != null && ! sab.fontWeight.equals( "" ) )
084 {
085 contents.append( "font-weight: " ).append( sab.fontWeight ).append( ";\n" );
086 }
087 if ( sab.fontSize != null && ! sab.fontSize.equals( "" ) )
088 {
089 contents.append( "font-size: " ).append( sab.fontSize ).append( ";\n" );
090 }
091 if ( sab.backgroundColor != null && ! sab.backgroundColor.equals( "" ) )
092 {
093 contents.append( "background-color: " ).append( sab.backgroundColor ).append( ";\n" );
094 }
095 if ( sab.textAlign != null && ! sab.textAlign.equals( "" ) )
096 {
097 contents.append( "text-align: " ).append( sab.textAlign ).append( ";\n" );
098 }
099 if ( sab.verticalAlign != null && ! sab.verticalAlign.equals( "" ) )
100 {
101 contents.append( "vertical-align: " ).append( sab.verticalAlign ).append( ";\n" );
102 }
103 if ( sab.width != null && ! sab.width.equals( "" ) )
104 {
105 contents.append( "width: " ).append( sab.width ).append( ";\n" );
106 }
107 if ( sab.height != null && ! sab.height.equals( "" ) )
108 {
109 contents.append( "height: " ).append( sab.height ).append( ";\n" );
110 }
111 if ( sab.borderStyle != null && ! sab.borderStyle.equals( "" ) )
112 {
113 contents.append( "border-style: " ).append( sab.borderStyle ).append( ";\n" );
114 }
115 if ( sab.borderWidth != null && ! sab.borderWidth.equals( "" ) )
116 {
117 contents.append( "border-width: " ).append( sab.borderWidth ).append( ";\n" );
118 }
119 if ( sab.paddingTop > 0 )
120 {
121 contents.append( "padding-top: " ).append( sab.paddingTop ).append( ";\n" );
122 }
123 if ( sab.paddingLeft > 0 )
124 {
125 contents.append( "padding-left: " ).append( sab.paddingLeft ).append( ";\n" );
126 }
127 if ( sab.paddingRight > 0 )
128 {
129 contents.append( "padding-right: " ).append( sab.paddingRight ).append( ";\n" );
130 }
131 if ( sab.paddingBottom > 0 )
132 {
133 contents.append( "padding-bottom: " ).append( sab.paddingBottom ).append( ";\n" );
134 }
135 if ( sab.textDecoration != null && ! sab.textDecoration.equals( "" ) )
136 {
137 contents.append( "text-decoration: " ).append( sab.textDecoration ).append( ";\n" );
138 }
139
140 contents.append( "}\n" );
141 }
142
143 cssContents = contents.toString();
144 }
145
146 /**
147 * Write the contents of {@link #cssContents} to the file specified.
148 *
149 * @param fileName - The fully qualified name of the file (ie.
150 * the file with full path - absolute or relative desired) to which
151 * the CSS contents are to be written.
152 * @throws FileNotFoundException - If the specified file could not
153 * be opened for read/write
154 */
155 public void writeToFile( String fileName )
156 throws FileNotFoundException
157 {
158 PrintWriter writer = new PrintWriter( new FileOutputStream( fileName ) );
159 writer.print( cssContents );
160 writer.flush();
161 writer.close();
162 }
163
164 /**
165 * Returns {@link #cssContents}.
166 *
167 * @return String - The value/reference of/to cssContents.
168 */
169 public final String getCssContents()
170 {
171 return cssContents;
172 }
173 }