KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > acting > ValidateAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: ValidateAction.java 42727 2004-03-18 14:51:58Z egli $ */
19
20 package org.apache.lenya.cms.cocoon.acting;
21
22 import java.io.File JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
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 JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45
46 /**
47  * Action to validate an xml document with relax ng schema.
48  */

49 public class ValidateAction extends AbstractConfigurableAction {
50     Category log = Category.getInstance(ValidateAction.class);
51
52     /** (non-Javadoc)
53      * @see org.apache.cocoon.acting.Action#act(org.apache.cocoon.environment.Redirector, org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
54      **/

55     public Map JavaDoc act(
56         Redirector redirector,
57         SourceResolver resolver,
58         Map JavaDoc objectModel,
59         String JavaDoc source,
60         Parameters parameters)
61         throws Exception JavaDoc {
62         File JavaDoc sitemap = new File JavaDoc(new URL JavaDoc(resolver.resolveURI("").getURI()).getFile());
63         File JavaDoc schema =
64             new File JavaDoc(
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 JavaDoc tmpFile = createTmpFile(request.getParameter("content"));
83             String JavaDoc message = validateDocument(schema, tmpFile);
84             tmpFile.delete();
85             if (message != null) {
86                 HashMap JavaDoc hmap = new HashMap JavaDoc();
87                 hmap.put("message", "RELAX NG Validation failed: " + message);
88                 return hmap;
89             }
90         } catch (Exception JavaDoc e) {
91             // FIXME: could it be that the tmpFile is not removed in the case of
92
// an exception? Exceptions happen everytime the validation fails
93
getLogger().error("RELAX NG Validation failed: " + e.getMessage());
94             HashMap JavaDoc hmap = new HashMap JavaDoc();
95             hmap.put("message", "RELAX NG Validation failed: " + e.getMessage());
96             return hmap;
97         }
98         return null;
99     }
100
101     /**
102      * Validate document
103      * @param schema The relax ng schema.
104      * @param file The file to validate
105      * @return The validation error message or null.
106      */

107     private String JavaDoc validateDocument(File JavaDoc schema, File JavaDoc file) throws Exception JavaDoc {
108         return RelaxNG.validate(schema, file);
109
110     }
111
112     private File JavaDoc createTmpFile(String JavaDoc content)
113         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, TransformerException JavaDoc, IOException JavaDoc {
114         File JavaDoc tmpFile = File.createTempFile("OneformEditor", null);
115         getLogger().debug("file: " + tmpFile.getAbsolutePath());
116
117         //write POST content in temporary file
118
FileWriter JavaDoc fileWriter = new FileWriter JavaDoc(tmpFile);
119         fileWriter.write(content);
120         fileWriter.close();
121
122         Document JavaDoc document = null;
123         DocumentBuilderFactory JavaDoc parserFactory = DocumentBuilderFactory.newInstance();
124         parserFactory.setValidating(false);
125         parserFactory.setNamespaceAware(true);
126         parserFactory.setIgnoringElementContentWhitespace(true);
127         DocumentBuilder JavaDoc 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