001 package com.javaworld.apr2000.sax;
002
003 /**
004 * Sample code by Mark Johnson, JavaWorld, April 2000.
005 * Code is may be used for any legal purpose, including commercial
006 * purposes, with no warranty expressed or implied.
007 * email: mark.johnson@javaworld.com
008 */
009
010 import org.xml.sax.*;
011 import java.io.*;
012
013 public class RecipeWriter {
014 private String _sName = "";
015 private String _sQty = null;
016 private boolean _isOptional = false;
017 private boolean _isVegetarian = true;
018 private PrintWriter _pwOut = null;
019 /**
020 * ShoppingListWriter constructor comment.
021 */
022 public RecipeWriter() {
023 super();
024 }
025 /**
026 * ShoppingListWriter constructor comment.
027 */
028 public RecipeWriter(String sOutfile) {
029 super();
030
031 File fOutfile = new File(sOutfile);
032 sOutfile = fOutfile.getAbsolutePath();
033
034 try {
035 _pwOut = new PrintWriter(new FileWriter(sOutfile));
036 } catch (IOException ex) {
037 System.err.println("IO error: " + ex);
038 System.err.println("Writing to standard output");
039 }
040 }
041 /**
042 * End description text style
043 */
044 public void endDescription() {
045 textEnd();
046 }
047 /**
048 * End document
049 */
050 public void endIngredients() {
051 textEnd();
052 }
053 /**
054 * Terminate instruction list.
055 */
056 public void endInstructions() {
057 println("</OL>");
058 textEnd();
059 }
060 /**
061 * Finish up recipe HTML
062 */
063 public void endRecipe() {
064 println("</TABLE></BODY></HTML>");
065
066 println("<P><P><UL>");
067 if (_isVegetarian) {
068 println("<img src=\"Cow.gif\" ALIGN=\"LEFT\"><font color=\"green\"><P><P><B>This recipe <i>is</i> vegetarian</B></font>");
069 } else {
070 println("<img src=\"Allig8r.gif\" ALIGN=\"LEFT\"><font color=\"red\"><P><P><B>This recipe <i>is not</i> vegetarian</B></font>");
071 }
072 println("</UL>");
073
074 if (_pwOut != null)
075 _pwOut.close();
076 }
077 /**
078 * Finish up a step.
079 */
080 public void endStep() {
081 println("</LI>");
082 }
083 /*
084 * Setup title color and size
085 */
086 public void headerPrint(String sHeader) {
087 println("<TR VALIGN=\"middle\">" +
088 "<TD BACKGROUND=\"redpaper.jpg\" COLSPAN=\"2\" HEIGHT=\"40\">" +
089 "<FONT COLOR=\"#EFD7BF\" SIZE=\"+2\"><B> ");
090 println(sHeader);
091 println("</B></font></TD></TR>");
092 }
093 /**
094 * Print a string to the current output stream.
095 * @param s java.lang.String
096 */
097 public void print(String s) {
098 if (_pwOut == null) {
099 System.out.print(s);
100 } else {
101 _pwOut.print(s);
102 }
103 }
104 /**
105 * Print a string to the current output stream.
106 * @param s java.lang.String
107 */
108 public void println(String s) {
109 if (_pwOut == null) {
110 System.out.println(s);
111 } else {
112 _pwOut.println(s);
113 }
114 }
115 /**
116 * Print description header and start text style
117 */
118 public void startDescription() {
119 headerPrint("Description");
120 textStart();
121 }
122 /**
123 * Detect the presence of a nonvegetarian ingredient.
124 */
125 public void startIngredient(AttributeList al) {
126 String sVegetarian = al.getValue("vegetarian");
127 if (sVegetarian != null && (sVegetarian.equals("0") || sVegetarian.equals("false"))) {
128 _isVegetarian = false;
129 }
130 }
131 /**
132 * Print title for shopping list
133 */
134 public void startIngredients() {
135 headerPrint("Ingredients");
136 textStart();
137 }
138 /**
139 * Set up instruction list
140 */
141 public void startInstructions() {
142 headerPrint("Instructions");
143 textStart();
144 println("<OL>");
145 }
146 /**
147 * Records whether the item is optional.
148 * @param alAttrs org.xml.sax.AttributeList
149 */
150 public void startItem(AttributeList alAttrs) {
151 String sOptional = alAttrs.getValue("optional");
152 if (sOptional != null && (sOptional.equals("true") || sOptional.equals("1"))) {
153 _isOptional = true;
154 } else {
155 _isOptional = false;
156 }
157 }
158 /**
159 * Save the "units" attribute for the current quantity.
160 */
161 public void startQty(AttributeList al) {
162 String sUnits = al.getValue("unit");
163
164 if (sUnits != null) {
165 _sQty = sUnits;
166 }
167 }
168 /**
169 * Print a step
170 */
171 public void startStep() {
172 print("<LI>");
173 }
174 /**
175 * End a block of text
176 */
177 public void textEnd() {
178 println("</B></FONT></TD></TR>");
179 println("<TR><TD COLSPAN=\"2\" HEIGHT=\"25\"> </TD></TR>");
180 }
181 /**
182 * Print descriptive text for recipe
183 */
184 public void textOfDescription(String sDesc) {
185 print(HtmlEncoder.sEncode(sDesc));
186 }
187 /**
188 * Report the current ingredient item.
189 */
190 public void textOfItem(String sText) {
191 sText = sText.trim();
192 if (_sQty != null) {
193 print(HtmlEncoder.sEncode(_sQty) + " ");
194 }
195 print(HtmlEncoder.sEncode(sText));
196 if (_isOptional) {
197 println(" <i>(optional)</i><br>");
198 } else {
199 println("<br>");
200 }
201 _sQty = null;
202 }
203 /**
204 * Print title for recipe
205 */
206 public void textOfName(String sName) {
207 titlePrint(sName);
208 }
209 /**
210 * Save the ingredient quantity for later reporting. If it's empty,
211 * the entire ingredient is empty.
212 */
213 public void textOfQty(String sText) {
214 sText = sText.trim();
215 if (sText.length() > 0) {
216 if (_sQty == null) {
217 _sQty = sText;
218 } else {
219 _sQty = sText + " " + _sQty;
220 }
221 } else {
222 _sQty = " ";
223 }
224 }
225 /**
226 * Print the text for a recipe step.
227 * @param sText java.lang.String
228 */
229 public void textOfStep(String sText) {
230 print(HtmlEncoder.sEncode(sText));
231 }
232 /**
233 * End a block of text
234 */
235 public void textStart() {
236 println("<TR><TD WIDTH=\"25\"> </TD><TD><FONT COLOR=\"#804040\"><B>");
237 }
238 /*
239 * Setup title color and size
240 */
241 public void titlePrint(String sTitle) {
242 println("<HTML><HEAD><TITLE>" + sTitle + "</TITLE>");
243 println("<BODY BACKGROUND=\"creampaper.jpg\">");
244 println("<CENTER><FONT COLOR=\"#804040\" SIZE=\"+3\"><B><I>");
245 println(sTitle);
246 println("</I></B></FONT></CENTER>");
247 println("<TABLE BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" WIDTH=\"100%\">");
248 }
249 }