KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > editor > EditTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.editor;
11
12 import javax.servlet.jsp.*;
13
14 import java.lang.reflect.Method JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.net.URLConnection JavaDoc;
21 import java.util.*;
22
23 import org.mmbase.bridge.*;
24 import org.mmbase.util.logging.Logger;
25 import org.mmbase.util.logging.Logging;
26
27 import org.mmbase.util.*;
28 import org.mmbase.util.xml.*;
29 import org.mmbase.util.functions.Parameters;
30
31 import org.mmbase.bridge.jsp.taglib.util.Attribute;
32 import org.mmbase.bridge.jsp.taglib.*;
33
34 import org.w3c.dom.Element JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36
37 /**
38  * The EditTag collects the nodenrs, fields and queries of the FieldTags in its body.
39  * FieldTags register these with the EditTag. You can pass these values to an
40  * editor by creating an url for example like YAMMEditor does which gives access to
41  * the editor yammeditor.jsp.<br />
42  * If you like you can extend Editor to create your own way to edit MMBase content.
43  * Edit the edittag.xml resources file to add your class in the MMBase config/taglib
44  * directory to let the EditTag know about it. The class BasicEditor gives an example how
45  * to generate an url to the basic jsp editors and my_editors.
46  *
47  *
48  * @author Andr&eacute; van Toly
49  * @author Michiel Meeuwissen
50  * @version $Id: EditTag.java,v 1.17.2.1 2006/12/05 21:56:58 michiel Exp $
51  * @see Editor
52  * @see BasicEditor
53  * @see YAMMEditor
54  * @since MMBase-1.8
55  */

56 public class EditTag extends CloudReferrerTag implements ParamHandler {
57
58     private static final Logger log = Logging.getLoggerInstance(EditTag.class);
59     private static final Map edittagTypes = new HashMap(); // edittagtype -> editordefinition
60

61     static {
62         try {
63             org.mmbase.util.XMLEntityResolver.registerPublicID("-//MMBase//DTD edittagtypes 1.0//EN", "edittagtypes_1_0.dtd", EditTag.class);
64             ResourceWatcher watcher = new ResourceWatcher(ResourceLoader.getConfigurationRoot().getChildResourceLoader("taglib")) {
65                 public void onChange(String JavaDoc resource) {
66                     edittagTypes.clear();
67                     // default: reading from taglib jar in case no other resources exist
68
InputStream JavaDoc stream = EditTag.class.getResourceAsStream("resources/edittag.xml");
69                      if (stream != null) { // fallback in case config/taglib may not exist
70
log.service("Reading default edittag resource: " + EditTag.class.getName() + "/resources/edittag.xml");
71                          InputSource JavaDoc ettypes = new InputSource JavaDoc(stream);
72                          readXML(ettypes);
73                      }
74
75
76                     ResourceLoader taglibLoader = ResourceLoader.getConfigurationRoot().getChildResourceLoader("taglib");
77                     List resources = taglibLoader.getResourceList(resource);
78                     log.service("Reading edittag resources: " + resources);
79                     ListIterator i = resources.listIterator();
80                     while (i.hasNext()) {
81                         try {
82                             URL JavaDoc u = (URL JavaDoc) i.next();
83                             URLConnection JavaDoc con = u.openConnection();
84                             if (con.getDoInput()) {
85                                 log.debug("Reading edittag resource: " + u);
86                                 InputSource JavaDoc source = new InputSource JavaDoc(con.getInputStream());
87                                 readXML(source);
88                             } else {
89                                 log.debug("Unavailable Edittag resource: " + u);
90                             }
91                         } catch (Exception JavaDoc e) {
92                             log.error("Error connecting or resource not found: " + e);
93                         }
94                     }
95                     ArrayList l = new ArrayList(edittagTypes.keySet());
96                     Collections.sort(l);
97                     log.service("Found edit-tag types " + l);
98                 }
99             };
100             watcher.add("edittag.xml");
101             watcher.start();
102             watcher.onChange("edittag.xml");
103
104         } catch (Exception JavaDoc e){
105             log.error(e.toString());
106         }
107     }
108
109     /**
110      * 'reads' a resource XML and puts its values in a Map with types of edittags
111      *
112      */

113     protected static void readXML(InputSource JavaDoc edittagSource) {
114         DocumentReader reader = new DocumentReader(edittagSource, EditTag.class);
115         Element JavaDoc root = reader.getElementByPath("edittagtypes");
116
117         Iterator i = reader.getChildElements(root, "editor");
118         while (i.hasNext()) {
119             Element JavaDoc element = (Element JavaDoc) i.next();
120             String JavaDoc type = element.getAttribute("type");
121
122             try {
123                 EditorDefinition newDef = new EditorDefinition(element);
124                 Object JavaDoc original = edittagTypes.put(type, newDef);
125                 if (original != null) {
126                     log.service("Replaced edittag '" + type + "' " + original + " --> " + newDef);
127                 } else {
128                     log.debug("Created edittag '" + type + "': " + newDef);
129                 }
130             } catch (Exception JavaDoc e) {
131                 log.error("In: " + XMLWriter.write(element, true) + ": " + e.getClass() + " " + e.getMessage());
132             }
133         }
134
135     }
136
137     private Attribute type = Attribute.NULL;
138
139     private Query query;
140     private int nodenr;
141     private String JavaDoc fieldName;
142
143
144     private Editor editor = null; // should do all the work
145

146     /**
147      * The type of editor, add your own editor in config/taglib/edittag.xml.
148      *
149      * @param t String with editor type.
150      */

151     public void setType(String JavaDoc t) throws JspTagException {
152         type = getAttribute(t);
153     }
154
155     /**
156      * The type of editor, see config/taglib/edittag.xml for different types or
157      * add your own editor.
158      *
159      * @return String stating the type of editor the tag refers to.
160      */

161     public String JavaDoc getType() throws JspTagException {
162         if (type == Attribute.NULL) {
163             return "edittag"; // the default
164
} else {
165             return type.getString(this);
166         }
167     }
168
169     /**
170      * Parameters of this tag. The tag's type is defined in its attribute, but you
171      * can give it as much parameters as you like to controll your implementations
172      * behaviour.
173      *
174      * @param key String with a key
175      * @param value Object with a value
176      */

177     public void addParameter(String JavaDoc key, Object JavaDoc value) throws JspTagException {
178         if (log.isDebugEnabled()) log.debug("adding parameter " + key + "/" + value);
179         editor.getParameters().set(key, value);
180     }
181
182     /**
183      * Start the EditTag, put the implementations found in its resources in a Map,
184      * consult the attribute type which implementation to use and instantiate it.
185      *
186      */

187     public int doStartTag() throws JspTagException {
188         if (log.isDebugEnabled()) log.debug("doStartTag of EditTag");
189
190
191         EditorDefinition def = (EditorDefinition) edittagTypes.get(getType());
192         if (def == null) {
193             throw new JspTagException("'" + getType() + "' is not a known edit type. Known are " + edittagTypes.keySet());
194         }
195         if (log.isDebugEnabled()) {
196             log.debug("Using editor: " + def);
197         }
198         editor = def.newInstance();
199
200         return EVAL_BODY;
201     }
202
203     /**
204      * Pass all gathered information to the implementing editor, get the
205      * the result back and write it to the page.
206      *
207      */

208     public int doEndTag() throws JspTagException {
209
210         fillStandardParameters(editor.getParameters());
211         editor.getParameters().checkRequiredParameters();
212         try {
213             editor.getEditorHTML(getPageContext());
214         } catch (IOException JavaDoc ioe) {
215             log.error("Error writing to PageContext: " + ioe.getMessage(), ioe);
216         }
217         // for gc:
218
editor = null;
219         return super.doEndTag();
220     }
221
222     /**
223      * Here is were the FieldTag registers its fields and some associated
224      * and maybe usefull information with the EditTag.
225      *
226      * @param query SearchQuery object that delivered the field
227      * @param nodenr int with the number of the node the field belongs to
228      * @param fieldName String with the fieldname
229      */

230     public void registerField(Query query, int nodenr, String JavaDoc fieldName) {
231         if (log.isDebugEnabled()) {
232             log.debug("nodenr: " + nodenr);
233             log.debug("fieldName: " + fieldName);
234             log.debug("query: " + query);
235         }
236         editor.queryList.add(query);
237         editor.nodenrList.add(String.valueOf(nodenr));
238         editor.fieldList.add(fieldName);
239     }
240
241
242     // if EVAL_BODY == EVAL_BODY_BUFFERED
243
public int doAfterBody() throws JspTagException {
244         if (EVAL_BODY == EVAL_BODY_BUFFERED && bodyContent != null) {
245             try {
246                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
247             } catch (IOException JavaDoc ioe) {
248                 throw new TaglibException(ioe);
249             }
250         }
251         return SKIP_BODY;
252     }
253
254
255     static class EditorDefinition {
256         private final Class JavaDoc clazz;
257         private final Map params = new HashMap(); /* String -> String */
258
259         public EditorDefinition(Element JavaDoc element) throws ClassNotFoundException JavaDoc {
260             Class JavaDoc c = null;
261             org.w3c.dom.NodeList JavaDoc childNodes = element.getChildNodes();
262             for (int i = 0; i < childNodes.getLength(); i++) {
263                 if (childNodes.item(i) instanceof Element JavaDoc) {
264                     Element JavaDoc childElement = (Element JavaDoc) childNodes.item(i);
265                     if (childElement.getLocalName().equals("class")) {
266                         String JavaDoc className = DocumentReader.getNodeTextValue(childElement);
267                         c = Class.forName(className);
268                     } else if (childElement.getLocalName().equals("param")) {
269                         String JavaDoc name = childElement.getAttribute("name");
270                         String JavaDoc value = DocumentReader.getNodeTextValue(childElement);
271                         if (params.put(name, value) != null) {
272                             log.error("Parameter '" + name + "' is defined more than once in " + XMLWriter.write(element, true));
273                         }
274
275                     }
276                 }
277             }
278             clazz = c;
279
280         }
281
282         public Editor newInstance() {
283             Editor res = null;
284             try {
285                 res = (Editor) clazz.newInstance();
286             } catch (InstantiationException JavaDoc ie) {
287                 log.error("Unable to instantiate class '" + clazz + "': " + ie);
288             } catch (IllegalAccessException JavaDoc iae) {
289                 log.error("IllegalAccessException instantiating class " + clazz+ "': " + iae);
290             }
291             if (res != null && params.size() > 0) {
292                 Parameters editorParams = res.getParameters();
293                 editorParams.setAll(params);
294             }
295             return res;
296         }
297     }
298
299 }
300
Popular Tags