001    /*
002     * Copyright (c) 2005 Marko Karppinen & Co. LLC
003     *
004     * Permission is hereby granted, free of charge, to any person obtaining a 
005     * copy of this software and associated documentation files (the "Software"), 
006     * to deal in the Software without restriction, including without limitation 
007     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
008     * and/or sell copies of the Software, and to permit persons to whom the 
009     * Software is furnished to do so, subject to the following conditions:
010     *
011     * The above copyright notice and this permission notice shall be included in 
012     * all copies or substantial portions of the Software.
013     *
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
015     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
016     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
017     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
018     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
019     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
020     * DEALINGS IN THE SOFTWARE.
021     */
022    
023    package fi.karppinen.xml;
024    
025    import java.util.Locale;
026    
027    import org.xml.sax.ContentHandler;
028    import org.xml.sax.SAXException;
029    
030    import fi.iki.hsivonen.xml.XhtmlSaxEmitter;
031    
032    /**
033     * Additional methods to <code>XhtmlSaxEmitter</code>.
034     * 
035     * @version $Id: XhtmlEmitter.java,v 1.2 2006/12/01 12:37:00 hsivonen Exp $
036     * @author hsivonen
037     */
038    public class XhtmlEmitter extends XhtmlSaxEmitter {
039        protected AttributesImpl attrs = new AttributesImpl();
040        
041        /**
042         * @param contentHandler
043         */
044        public XhtmlEmitter(ContentHandler contentHandler) {
045            super(contentHandler);
046        }
047    
048        public void startElementWithClass(String name, String clazz) throws SAXException {
049            attrs.clear();
050            attrs.addAttribute("class", clazz);
051            startElement(name, attrs);
052        }
053    
054        public void headCell(String text, String align) throws SAXException {
055            attrs.clear();
056            attrs.addAttribute("align", align);
057            attrs.addAttribute("valign", "top");
058            startElement("th", attrs);
059            characters(text);
060            endElement("th");    
061        }
062    
063        public void textCell(String text, String align) throws SAXException {
064            attrs.clear();
065            attrs.addAttribute("align", align);
066            attrs.addAttribute("valign", "top");
067            startElement("td", attrs);
068            characters(text);
069            endElement("td");    
070        }
071    
072        
073        public void radioButton(String name, String value, boolean checked) throws SAXException {
074            attrs.clear();
075            attrs.addAttribute("name", name);
076            attrs.addAttribute("value", value);
077            attrs.addAttribute("type", "radio");
078            if(checked) {
079                attrs.addAttribute("checked", "checked");
080            }
081            startElement("input", attrs);
082            endElement("input");            
083        }
084        public void checkbox(String name, boolean checked) throws SAXException {
085            attrs.clear();
086            attrs.addAttribute("name", name);
087            attrs.addAttribute("value", "checked");
088            attrs.addAttribute("type", "checkbox");
089            if(checked) {
090                attrs.addAttribute("checked", "checked");
091            }
092            startElement("input", attrs);
093            endElement("input");            
094        }
095        public void radioButtonCell(String name, String value, boolean checked, String align) throws SAXException {
096            attrs.clear();
097            attrs.addAttribute("align", align);
098            attrs.addAttribute("valign", "top");
099            startElement("td", attrs);
100            radioButton(name, value, checked);
101            endElement("td");            
102        }
103        public void submitButton(String name, String value) throws SAXException {
104            attrs.clear();
105            attrs.addAttribute("name", name);
106            attrs.addAttribute("value", value);
107            attrs.addAttribute("type", "submit");
108            startElement("input", attrs);
109            endElement("input");            
110        }
111        public void textarea(String name, String value) throws SAXException {
112            attrs.clear();
113            attrs.addAttribute("name", name);
114            attrs.addAttribute("rows", "10");
115            attrs.addAttribute("cols", "40");
116            startElement("textarea", attrs);
117            characters(value);
118            endElement("textarea");            
119        }
120        public void startElement(String name, Locale lang) throws SAXException {
121            StringBuilder buf = new StringBuilder();
122            buf.append(lang.getLanguage());
123            String country = lang.getCountry();
124            if(country.length() > 1) {
125                buf.append('-');
126                buf.append(country);
127            }
128            AttributesImpl attrs = new AttributesImpl();
129            attrs.addAttribute("http://www.w3.org/XML/1998/namespace", "lang", "xml:lang", "CDATA", buf.toString());
130            super.startElement(name, attrs);
131        }
132    
133        /**
134         * @param string
135         * @param answer
136         * @throws SAXException
137         */
138        public void textInput(String name, String value) throws SAXException {
139            attrs.clear();
140            attrs.addAttribute("name", name);
141            attrs.addAttribute("value", value);
142            attrs.addAttribute("type", "text");
143            attrs.addAttribute("size", "40");
144            startElement("input", attrs);
145            endElement("input");            
146        }
147    
148        /**
149         * @param string
150         * @param answer
151         * @throws SAXException
152         */
153        public void hidden(String name, String value) throws SAXException {
154            attrs.clear();
155            attrs.addAttribute("name", name);
156            attrs.addAttribute("value", value);
157            attrs.addAttribute("type", "hidden");
158            startElement("input", attrs);
159            endElement("input");            
160        }
161    
162    }