1 16 17 18 19 20 package org.apache.lenya.cms.cocoon.acting; 21 22 import java.io.File ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import org.apache.avalon.framework.configuration.Configurable; 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 31 import org.apache.avalon.framework.parameters.ParameterException; 32 import org.apache.avalon.framework.parameters.Parameters; 33 import org.apache.cocoon.ProcessingException; 34 import org.apache.cocoon.acting.AbstractComplementaryConfigurableAction; 35 import org.apache.cocoon.environment.ObjectModelHelper; 36 import org.apache.cocoon.environment.Redirector; 37 import org.apache.cocoon.environment.Request; 38 import org.apache.cocoon.environment.Session; 39 import org.apache.cocoon.environment.SourceResolver; 40 import org.apache.lenya.cms.authoring.ParentChildCreatorInterface; 41 import org.apache.lenya.cms.publication.SiteTree; 42 import org.apache.lenya.cms.publication.Label; 43 import org.apache.lenya.cms.publication.Publication; 44 import org.apache.lenya.cms.publication.PublicationFactory; 45 import org.apache.log4j.Category; 46 47 48 import org.dom4j.Attribute; 49 import org.dom4j.Document; 50 import org.dom4j.io.SAXReader; 51 52 53 56 public class DefaultCreatorAction extends AbstractComplementaryConfigurableAction implements Configurable { 57 Category log = Category.getInstance(DefaultCreatorAction.class); 58 59 private String docsPath = null; 60 private String doctypesPath = null; 61 62 69 public void configure(Configuration conf) throws ConfigurationException { 70 super.configure(conf); 71 72 docsPath = conf.getChild("docs").getAttribute("href"); 73 doctypesPath = conf.getChild("doctypes").getAttribute("href"); 74 } 75 76 89 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String src, 90 Parameters parameters) throws Exception { 91 Publication publication = PublicationFactory.getPublication(objectModel); 92 93 Request request = ObjectModelHelper.getRequest(objectModel); 95 96 if (request == null) { 97 getLogger().error("No request object"); 98 99 return null; 100 } 101 102 String parentid = request.getParameter("properties.create.parent-id"); 105 log.debug("properties.create.parent-id = " + parentid); 106 107 String childid = request.getParameter("properties.create.child-id"); 109 log.debug("properties.create.child-id = " + childid); 110 111 String childname = request.getParameter("properties.create.child-name"); 113 log.debug("properties.create.child-name = " + childname); 114 115 String childtype = request.getParameter("properties.create.child-type"); 117 log.debug("properties.create.childtype = " + childtype); 118 119 String visible = request.getParameter("properties.create.visible"); 121 122 boolean visibleInNav = true; 123 if (visible.equals("no")){ 124 visibleInNav = false; 125 } 126 127 short childType; 128 if (childtype.equals("branch")) { 129 childType = ParentChildCreatorInterface.BRANCH_NODE; 130 } else if (childtype.equals("leaf")) { 131 childType = ParentChildCreatorInterface.LEAF_NODE; 132 } else { 133 log.error("No such child type: " + childtype); 134 return null; 135 } 136 137 138 139 String doctype = request.getParameter("properties.create.doctype"); 141 log.debug("poperties.create.doctype = " + doctype); 142 143 String language = request.getParameter("properties.create.language"); 145 log.debug("poperties.create.language = " + language); 146 147 148 149 150 if (!validate(parentid, childid, childname, childtype, doctype)) { 151 getLogger().error("Exception: Validation of parameters failed"); 152 153 return null; 154 } 155 156 Session session = request.getSession(true); 158 159 if (session == null) { 160 getLogger().error("No session object"); 161 162 return null; 163 } 164 165 ParentChildCreatorInterface creator = null; 167 String absoluteDoctypesPath = publication.getDirectory() + File.separator + doctypesPath; 168 Document doctypesDoc = new SAXReader().read("file:" + absoluteDoctypesPath + 169 "doctypes.xconf"); 170 Attribute creator_src = (Attribute) doctypesDoc.selectSingleNode("/doctypes/doc[@type='" + 171 doctype + "']/creator/@src"); 172 173 if (creator_src != null) { 174 log.info("Creator found for \"" + doctype + "\": " + creator_src.getName() + " " + creator_src.getPath() + " " + creator_src.getValue()); 175 176 Class creatorClass = Class.forName(creator_src.getValue()); 178 creator = (ParentChildCreatorInterface) creatorClass.newInstance(); 179 } else { 180 log.warn("No creator found for \"" + doctype + "\". DefaultBranchCreator will be taken."); 181 creator = new org.apache.lenya.cms.authoring.DefaultBranchCreator(); 182 } 183 184 getLogger().debug(".act(): Creator : " + creator.getClass().getName()); 185 186 DefaultConfigurationBuilder defaultConfigBuilder = new DefaultConfigurationBuilder(); 189 Configuration[] docTypeConfigs = defaultConfigBuilder.buildFromFile(absoluteDoctypesPath + 190 "doctypes.xconf").getChildren(); 191 192 Configuration doctypeConf = null; 193 194 for (int i = 0; i < docTypeConfigs.length; i++) { 195 String typeName = docTypeConfigs[i].getAttribute("type"); 196 197 if (typeName.equals(doctype)) { 198 doctypeConf = docTypeConfigs[i].getChild("creator", false); 199 } 200 } 201 202 creator.init(doctypeConf); 203 204 SiteTree siteTree = publication.getTree(Publication.AUTHORING_AREA); 206 Label[] labels = new Label[1]; 207 labels[0] = new Label(childname, language); 208 siteTree.addNode(parentid, creator.generateTreeId(childid, childType), labels, visibleInNav); 209 210 HashMap allParameters = new HashMap (); 215 String [] names = parameters.getNames(); 216 217 for (int i = 0; i < names.length; i++) { 218 String name = names[i]; 219 String value = null; 220 221 try { 222 value = parameters.getParameter(name); 223 } catch (ParameterException pe) { 224 value = null; 225 } 226 227 allParameters.put(name, value); 228 } 229 230 Enumeration requestParameters = request.getParameterNames(); 231 232 while (requestParameters.hasMoreElements()) { 233 String requestParameterName = (String ) requestParameters.nextElement(); 234 235 if (allParameters.containsKey(requestParameterName)) { 236 throw new ProcessingException("Name clash in request parameter " + 238 "and sitemap parameter: " + requestParameterName); 239 } 240 241 allParameters.put(requestParameterName, request.getParameter(requestParameterName)); 242 } 243 244 Enumeration sessionAttributeNames = session.getAttributeNames(); 245 246 while (sessionAttributeNames.hasMoreElements()) { 247 String sessionAttributeName = (String ) sessionAttributeNames.nextElement(); 248 249 if (allParameters.containsKey(sessionAttributeName)) { 250 throw new ProcessingException("Name clash in session attribute " + 252 "and request parameter or sitemap parameter: " + sessionAttributeName); 253 } 254 255 allParameters.put(sessionAttributeName, session.getAttribute(sessionAttributeName)); 256 } 257 258 try { 259 creator.create(new File (absoluteDoctypesPath + "samples"), 260 new File (publication.getDirectory(), docsPath + parentid), childid, childType, 261 childname, language, allParameters); 262 } catch (Exception e) { 263 log.error("Creator threw exception: " + e); 264 return null; 265 } 266 267 siteTree.save(); 269 270 HashMap actionMap = new HashMap (); 271 272 return actionMap; 273 } 274 275 286 public boolean validate(String parentid, String childid, String childname, String childtype, 287 String doctype) { 288 getLogger().debug(".validate(): parentid=" + parentid + " ; childid=" + childid + 289 " ; childname=" + childname + " ; childtype=" + childtype + " ; doctype=" + doctype); 290 291 if ((childid.indexOf(" ") >= 0) || (childid.length() == 0)) { 292 return false; 293 } 294 295 if (childname.length() == 0) { 296 return false; 297 } 298 299 return true; 300 } 301 } 302 | Popular Tags |