1 17 18 19 20 package org.apache.lenya.cms.cocoon.acting; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.OutputStreamWriter ; 25 import java.io.Writer ; 26 import java.net.URL ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.apache.avalon.framework.parameters.Parameters; 31 import org.apache.avalon.framework.thread.ThreadSafe; 32 import org.apache.cocoon.acting.AbstractConfigurableAction; 33 import org.apache.cocoon.environment.ObjectModelHelper; 34 import org.apache.cocoon.environment.Redirector; 35 import org.apache.cocoon.environment.SourceResolver; 36 import org.apache.cocoon.environment.http.HttpRequest; 37 import org.apache.lenya.xml.DocumentHelper; 38 import org.apache.lenya.xml.RelaxNG; 39 import org.apache.log4j.Category; 40 import org.xml.sax.SAXException ; 41 42 45 public class OneFormEditorSaveAction 46 extends AbstractConfigurableAction 47 implements ThreadSafe { 48 Category log = Category.getInstance(OneFormEditorSaveAction.class); 49 50 63 public Map act( 64 Redirector redirector, 65 SourceResolver resolver, 66 Map objectModel, 67 String source, 68 Parameters parameters) 69 throws Exception { 70 71 HttpRequest request = (HttpRequest)ObjectModelHelper.getRequest(objectModel); 72 73 String namespaces = removeRedundantNamespaces(request.getParameter("namespaces")); 75 log.debug(namespaces); 76 77 String encoding = request.getCharacterEncoding(); 79 String content = 80 "<?xml version=\"1.0\" encoding=\"" 81 + encoding 82 + "\"?>\n" 83 + addNamespaces(namespaces, request.getParameter("content")); 84 85 File sitemap = new File (new URL (resolver.resolveURI("").getURI()).getFile()); 87 File file = 88 new File ( 89 sitemap.getAbsolutePath() 90 + File.separator 91 + parameters.getParameter("file")); 92 93 File parentFile = new File (file.getParent()); 94 if (!parentFile.exists()) { 95 parentFile.mkdirs(); 96 } 97 FileOutputStream fileoutstream = new FileOutputStream (file); 98 Writer writer = new OutputStreamWriter (fileoutstream, encoding); 99 writer.write(content, 0, content.length()); 100 writer.close(); 101 102 File schema = 104 new File ( 105 sitemap.getAbsolutePath() 106 + File.separator 107 + parameters.getParameter("schema")); 108 if (schema.isFile()) { 109 110 try { 111 DocumentHelper.readDocument(file); 112 } 113 catch (SAXException e) { 114 log.error("Wellformedness check failed: " + e.getMessage()); 115 Map hmap = new HashMap (); 116 hmap.put("message", "Document is not well-formed: " + e.getMessage()); 117 return hmap; 118 } 119 120 String message = RelaxNG.validate(schema, file); 121 if (message != null) { 122 log.error("RELAX NG Validation failed: " + message); 123 Map hmap = new HashMap (); 124 hmap.put("message", "RELAX NG Validation failed: " + message); 125 return hmap; 126 } 127 } else { 128 log.warn( 129 "Will not be validated. No such schema: " + schema.getAbsolutePath()); 130 } 131 132 return null; 133 } 134 135 138 private String removeRedundantNamespaces(String namespaces) { 139 String [] namespace = namespaces.split(" "); 140 141 String ns = ""; 142 for (int i = 0; i < namespace.length; i++) { 143 if (ns.indexOf(namespace[i]) < 0) { 144 ns = ns + " " + namespace[i]; 145 } else { 146 log.debug("Redundant namespace: " + namespace[i]); 147 } 148 } 149 return ns; 150 } 151 152 155 private String addNamespaces(String namespaces, String content) { 156 int i = content.indexOf(">"); 157 return content.substring(0, i) + " " + namespaces + content.substring(i); 158 } 159 } 160 | Popular Tags |