1 17 18 19 20 package org.apache.lenya.cms.cocoon.acting; 21 22 import java.io.File ; 23 import java.io.FileWriter ; 24 import java.io.IOException ; 25 import java.net.URL ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.transform.TransformerException ; 33 34 import org.apache.avalon.framework.parameters.Parameters; 35 import org.apache.cocoon.acting.AbstractConfigurableAction; 36 import org.apache.cocoon.environment.ObjectModelHelper; 37 import org.apache.cocoon.environment.Redirector; 38 import org.apache.cocoon.environment.Request; 39 import org.apache.cocoon.environment.SourceResolver; 40 import org.apache.lenya.xml.DocumentHelper; 41 import org.apache.lenya.xml.RelaxNG; 42 import org.apache.log4j.Category; 43 import org.w3c.dom.Document ; 44 import org.xml.sax.SAXException ; 45 46 49 public class ValidateAction extends AbstractConfigurableAction { 50 Category log = Category.getInstance(ValidateAction.class); 51 52 55 public Map act( 56 Redirector redirector, 57 SourceResolver resolver, 58 Map objectModel, 59 String source, 60 Parameters parameters) 61 throws Exception { 62 File sitemap = new File (new URL (resolver.resolveURI("").getURI()).getFile()); 63 File schema = 64 new File ( 65 sitemap.getAbsolutePath() 66 + File.separator 67 + parameters.getParameter("schema")); 68 getLogger().debug("schema: " + schema.getAbsolutePath()); 69 70 Request request = ObjectModelHelper.getRequest(objectModel); 71 72 if (request.getParameter("cancel") != null) { 73 getLogger().warn(".act(): Editing has been canceled"); 74 return null; 75 } 76 if (!schema.isFile()) { 77 log.warn("No such schema: " + schema.getAbsolutePath()); 78 return null; 79 } 80 81 try { 82 File tmpFile = createTmpFile(request.getParameter("content")); 83 String message = validateDocument(schema, tmpFile); 84 tmpFile.delete(); 85 if (message != null) { 86 HashMap hmap = new HashMap (); 87 hmap.put("message", "RELAX NG Validation failed: " + message); 88 return hmap; 89 } 90 } catch (Exception e) { 91 getLogger().error("RELAX NG Validation failed: " + e.getMessage()); 94 HashMap hmap = new HashMap (); 95 hmap.put("message", "RELAX NG Validation failed: " + e.getMessage()); 96 return hmap; 97 } 98 return null; 99 } 100 101 107 private String validateDocument(File schema, File file) throws Exception { 108 return RelaxNG.validate(schema, file); 109 110 } 111 112 private File createTmpFile(String content) 113 throws SAXException , ParserConfigurationException , TransformerException , IOException { 114 File tmpFile = File.createTempFile("OneformEditor", null); 115 getLogger().debug("file: " + tmpFile.getAbsolutePath()); 116 117 FileWriter fileWriter = new FileWriter (tmpFile); 119 fileWriter.write(content); 120 fileWriter.close(); 121 122 Document document = null; 123 DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance(); 124 parserFactory.setValidating(false); 125 parserFactory.setNamespaceAware(true); 126 parserFactory.setIgnoringElementContentWhitespace(true); 127 DocumentBuilder builder = parserFactory.newDocumentBuilder(); 128 129 document = builder.parse(tmpFile.getAbsolutePath()); 130 131 DocumentHelper.writeDocument(document, tmpFile); 132 133 return tmpFile; 134 } 135 } 136 | Popular Tags |