1 10 package org.mmbase.bridge.jsp.taglib.editor; 11 12 import javax.servlet.jsp.*; 13 14 import java.lang.reflect.Method ; 15 import java.lang.reflect.InvocationTargetException ; 16 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.net.URL ; 20 import java.net.URLConnection ; 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 ; 35 import org.xml.sax.InputSource ; 36 37 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(); 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 resource) { 66 edittagTypes.clear(); 67 InputStream stream = EditTag.class.getResourceAsStream("resources/edittag.xml"); 69 if (stream != null) { log.service("Reading default edittag resource: " + EditTag.class.getName() + "/resources/edittag.xml"); 71 InputSource ettypes = new InputSource (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 u = (URL ) i.next(); 83 URLConnection con = u.openConnection(); 84 if (con.getDoInput()) { 85 log.debug("Reading edittag resource: " + u); 86 InputSource source = new InputSource (con.getInputStream()); 87 readXML(source); 88 } else { 89 log.debug("Unavailable Edittag resource: " + u); 90 } 91 } catch (Exception 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 e){ 105 log.error(e.toString()); 106 } 107 } 108 109 113 protected static void readXML(InputSource edittagSource) { 114 DocumentReader reader = new DocumentReader(edittagSource, EditTag.class); 115 Element root = reader.getElementByPath("edittagtypes"); 116 117 Iterator i = reader.getChildElements(root, "editor"); 118 while (i.hasNext()) { 119 Element element = (Element ) i.next(); 120 String type = element.getAttribute("type"); 121 122 try { 123 EditorDefinition newDef = new EditorDefinition(element); 124 Object 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 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 fieldName; 142 143 144 private Editor editor = null; 146 151 public void setType(String t) throws JspTagException { 152 type = getAttribute(t); 153 } 154 155 161 public String getType() throws JspTagException { 162 if (type == Attribute.NULL) { 163 return "edittag"; } else { 165 return type.getString(this); 166 } 167 } 168 169 177 public void addParameter(String key, Object value) throws JspTagException { 178 if (log.isDebugEnabled()) log.debug("adding parameter " + key + "/" + value); 179 editor.getParameters().set(key, value); 180 } 181 182 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 208 public int doEndTag() throws JspTagException { 209 210 fillStandardParameters(editor.getParameters()); 211 editor.getParameters().checkRequiredParameters(); 212 try { 213 editor.getEditorHTML(getPageContext()); 214 } catch (IOException ioe) { 215 log.error("Error writing to PageContext: " + ioe.getMessage(), ioe); 216 } 217 editor = null; 219 return super.doEndTag(); 220 } 221 222 230 public void registerField(Query query, int nodenr, String 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 public int doAfterBody() throws JspTagException { 244 if (EVAL_BODY == EVAL_BODY_BUFFERED && bodyContent != null) { 245 try { 246 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 247 } catch (IOException ioe) { 248 throw new TaglibException(ioe); 249 } 250 } 251 return SKIP_BODY; 252 } 253 254 255 static class EditorDefinition { 256 private final Class clazz; 257 private final Map params = new HashMap(); 258 259 public EditorDefinition(Element element) throws ClassNotFoundException { 260 Class c = null; 261 org.w3c.dom.NodeList childNodes = element.getChildNodes(); 262 for (int i = 0; i < childNodes.getLength(); i++) { 263 if (childNodes.item(i) instanceof Element ) { 264 Element childElement = (Element ) childNodes.item(i); 265 if (childElement.getLocalName().equals("class")) { 266 String className = DocumentReader.getNodeTextValue(childElement); 267 c = Class.forName(className); 268 } else if (childElement.getLocalName().equals("param")) { 269 String name = childElement.getAttribute("name"); 270 String 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 ie) { 287 log.error("Unable to instantiate class '" + clazz + "': " + ie); 288 } catch (IllegalAccessException 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 |