001: /*
002: * Created on Feb 28, 2005
003: */
004: package com.sun.portal.wireless.htmlconversion.processors;
005:
006: import java.util.HashMap;
007:
008: import org.w3c.dom.Element;
009:
010: import com.sun.portal.wireless.htmlconversion.ParserState;
011: import com.sun.portal.wireless.htmlconversion.TagProcessor;
012: import com.sun.portal.wireless.htmlconversion.TagProcessorRegistry;
013:
014: /**
015: * Tag processor for the HTML Input tag.
016: *
017: * This is an unusual implementation, since it's for a HTML, rather than an AML
018: * tag. The reason is that the HTML Input tag may cause creation of several
019: * different AML tags - AmlInput, AmlCheckBox, AmlChoice, AmlOption, AmlTextArea.
020: * Implementation of TagProcessor for each of these elements will be looked up
021: * in TagProcessorRegistry and delegated to for each of the methods defined on
022: * the TagProcessor interface.
023: *
024: * The HTML Input tag cannot contain any other tags or text, and is therefore
025: * a "simple" tag as defined by HTMLEditorKit.ParserCallback.
026: *
027: * @author ashwin.mathew@sun.com
028: */
029: public class HtmlInputTagProcessor extends BaseTagProcessor {
030:
031: // HTML Input tag attributes
032: public static final String ATTR_NAME = "name";
033: public static final String ATTR_TYPE = "type";
034: public static final String ATTR_VALUE = "value";
035: public static final String ATTR_SIZE = "size";
036: public static final String ATTR_MAXLENGTH = "maxlength";
037: public static final String ATTR_DISABLED = "disabled";
038: public static final String ATTR_READONLY = "readonly";
039: public static final String ATTR_CHECKED = "checked";
040:
041: // Values for type attribute
042: // Note that this is not the complete set of values
043: // possible in HTML - image, file and button are excluded
044: // since they are not supported for AML conversion.
045: public static final String VAL_TYPE_TEXT = "text";
046: public static final String VAL_TYPE_PASSWORD = "password";
047: public static final String VAL_TYPE_HIDDEN = "hidden";
048: public static final String VAL_TYPE_CHECKBOX = "checkbox";
049: public static final String VAL_TYPE_RADIO = "radio";
050: public static final String VAL_TYPE_RESET = "reset";
051: public static final String VAL_TYPE_SUBMIT = "submit";
052:
053: public static final String VAL_CHECKED_TRUE = "true";
054: public static final String VAL_CHECKED_FALSE = "false";
055:
056: private static final String[] supportedTags = { "input" };
057:
058: // Mapping of Input 'type' attribute to AML tag name
059: // to look up tag processor implementations
060: private static final HashMap typeMap = new HashMap();
061:
062: // List of TagProcessors that this class is allowed to invoke
063: private static final String[] allowedInputProcessors = new String[5];
064:
065: static {
066: typeMap.put(VAL_TYPE_TEXT, AmlInputTagProcessor.AML_INPUT);
067: typeMap.put(VAL_TYPE_PASSWORD, AmlInputTagProcessor.AML_INPUT);
068: typeMap.put(VAL_TYPE_HIDDEN, AmlInputTagProcessor.AML_INPUT);
069: typeMap.put(VAL_TYPE_SUBMIT,
070: AmlFormSubmitTagProcessor.AML_FORM_SUBMIT);
071: typeMap.put(VAL_TYPE_RESET,
072: AmlFormResetTagProcessor.AML_FORM_RESET);
073: typeMap.put(VAL_TYPE_CHECKBOX,
074: AmlCheckBoxTagProcessor.AML_CHECKBOX);
075: typeMap.put(VAL_TYPE_RADIO, HtmlRadioTagProcessor.HTML_RADIO);
076:
077: allowedInputProcessors[0] = AmlInputTagProcessor.AML_INPUT;
078: allowedInputProcessors[1] = AmlFormSubmitTagProcessor.AML_FORM_SUBMIT;
079: allowedInputProcessors[2] = AmlFormResetTagProcessor.AML_FORM_RESET;
080: allowedInputProcessors[3] = AmlCheckBoxTagProcessor.AML_CHECKBOX;
081: allowedInputProcessors[4] = HtmlRadioTagProcessor.HTML_RADIO;
082: }
083:
084: /* (non-Javadoc)
085: * @see com.sun.portal.wireless.htmlconversion.TagProcessor#getAmlTag()
086: */
087: public String getAmlTag() {
088: // Since it generates several different AML tags
089: return null;
090: }
091:
092: /* (non-Javadoc)
093: * @see com.sun.portal.wireless.htmlconversion.TagProcessor#getSupportedTags()
094: */
095: public String[] getSupportedTags() {
096: return supportedTags;
097: }
098:
099: /* (non-Javadoc)
100: * @see com.sun.portal.wireless.htmlconversion.TagProcessor#canHaveChildren()
101: */
102: public boolean canHaveChildren(ParserState state) {
103: TagProcessor processor = findProcessor(state);
104:
105: if (processor != null) {
106: return findProcessor(state).canHaveChildren(state);
107: } else {
108: return false;
109: }
110: }
111:
112: /* (non-Javadoc)
113: * @see com.sun.portal.wireless.htmlconversion.TagProcessor#startTag(java.lang.String, java.util.HashMap, com.sun.portal.wireless.htmlconversion.ParserState)
114: */
115: public Element startTag(String tagName, HashMap attributes,
116: ParserState state) {
117: String type = (String) attributes.get(ATTR_TYPE);
118:
119: if (type == null) {
120: // Default to text
121: type = VAL_TYPE_TEXT;
122: }
123:
124: TagProcessor processor = findProcessor(type, state);
125:
126: if (processor != null) {
127: return findProcessor(type, state).startTag(tagName,
128: attributes, state);
129: } else {
130: return null;
131: }
132: }
133:
134: /* (non-Javadoc)
135: * @see com.sun.portal.wireless.htmlconversion.TagProcessor#interruptedTag(com.sun.portal.wireless.htmlconversion.ParserState)
136: */
137: public Element interruptedTag(ParserState state) {
138: TagProcessor processor = findProcessor(state);
139:
140: if (processor != null) {
141: return findProcessor(state).interruptedTag(state);
142: } else {
143: return null;
144: }
145: }
146:
147: /* (non-Javadoc)
148: * @see com.sun.portal.wireless.htmlconversion.TagProcessor#endTag(java.lang.String, com.sun.portal.wireless.htmlconversion.ParserState)
149: */
150: public Element endTag(String tag, ParserState state) {
151: TagProcessor processor = findProcessor(state);
152:
153: if (processor != null) {
154: return findProcessor(state).endTag(tag, state);
155: } else {
156: return null;
157: }
158: }
159:
160: /* (non-Javadoc)
161: * @see com.sun.portal.wireless.htmlconversion.TagProcessor#isMaintainOnState()
162: */
163: public boolean isMaintainOnState(ParserState state) {
164: TagProcessor processor = findProcessor(state);
165:
166: if (processor != null) {
167: return findProcessor(state).isMaintainOnState(state);
168: } else {
169: return false;
170: }
171: }
172:
173: private TagProcessor findProcessor(ParserState state) {
174: return findProcessor(null, state);
175: }
176:
177: // Finds the TagProcessor implementation corresponding to
178: // the current HTML input tag type.
179: private TagProcessor findProcessor(String type, ParserState state) {
180: TagProcessor processor = null;
181:
182: String amlTag = null;
183:
184: if (type == null) {
185: Element currentOutputTag = state.getCurrentOutputTag();
186: amlTag = currentOutputTag.getTagName();
187: } else {
188: amlTag = (String) typeMap.get(type);
189: }
190:
191: boolean isAllowedProcessor = false;
192:
193: if (amlTag != null) {
194: for (int i = 0; i < allowedInputProcessors.length; i++) {
195: if (amlTag.equals(allowedInputProcessors[i])) {
196: isAllowedProcessor = true;
197: break;
198: }
199: }
200:
201: if (isAllowedProcessor) {
202: processor = TagProcessorRegistry.getInstance()
203: .getProcessor(amlTag);
204: }
205: }
206:
207: return processor;
208: }
209:
210: }
|