KickJava   Java API By Example, From Geeks To Geeks.

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


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: OneFormEditorSaveAction.java 76111 2004-11-17 11:19:53Z andreas $ */
19
20 package org.apache.lenya.cms.cocoon.acting;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
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 JavaDoc;
41
42 /**
43  *
44  */

45 public class OneFormEditorSaveAction
46     extends AbstractConfigurableAction
47     implements ThreadSafe {
48     Category log = Category.getInstance(OneFormEditorSaveAction.class);
49
50     /**
51      * Save data to temporary file
52      *
53      * @param redirector a <code>Redirector</code> value
54      * @param resolver a <code>SourceResolver</code> value
55      * @param objectModel a <code>Map</code> value
56      * @param source a <code>String</code> value
57      * @param parameters a <code>Parameters</code> value
58      *
59      * @return a <code>Map</code> value
60      *
61      * @exception Exception if an error occurs
62      */

63     public Map JavaDoc act(
64         Redirector redirector,
65         SourceResolver resolver,
66         Map JavaDoc objectModel,
67         String JavaDoc source,
68         Parameters parameters)
69         throws Exception JavaDoc {
70
71         HttpRequest request = (HttpRequest)ObjectModelHelper.getRequest(objectModel);
72
73         // Get namespaces
74
String JavaDoc namespaces = removeRedundantNamespaces(request.getParameter("namespaces"));
75         log.debug(namespaces);
76
77         // Aggregate content
78
String JavaDoc encoding = request.getCharacterEncoding();
79         String JavaDoc content =
80             "<?xml version=\"1.0\" encoding=\""
81                 + encoding
82                 + "\"?>\n"
83                 + addNamespaces(namespaces, request.getParameter("content"));
84
85         // Save file temporarily
86
File JavaDoc sitemap = new File JavaDoc(new URL JavaDoc(resolver.resolveURI("").getURI()).getFile());
87         File JavaDoc file =
88             new File JavaDoc(
89                 sitemap.getAbsolutePath()
90                     + File.separator
91                     + parameters.getParameter("file"));
92
93         File JavaDoc parentFile = new File JavaDoc(file.getParent());
94         if (!parentFile.exists()) {
95             parentFile.mkdirs();
96         }
97         FileOutputStream JavaDoc fileoutstream = new FileOutputStream JavaDoc(file);
98         Writer JavaDoc writer = new OutputStreamWriter JavaDoc(fileoutstream, encoding);
99         writer.write(content, 0, content.length());
100         writer.close();
101
102         // Validate
103
File JavaDoc schema =
104             new File JavaDoc(
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 JavaDoc e) {
114                 log.error("Wellformedness check failed: " + e.getMessage());
115                 Map JavaDoc hmap = new HashMap JavaDoc();
116                 hmap.put("message", "Document is not well-formed: " + e.getMessage());
117                 return hmap;
118             }
119             
120             String JavaDoc message = RelaxNG.validate(schema, file);
121             if (message != null) {
122                 log.error("RELAX NG Validation failed: " + message);
123                 Map JavaDoc hmap = new HashMap JavaDoc();
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     /**
136      * Remove redundant namespaces
137      */

138     private String JavaDoc removeRedundantNamespaces(String JavaDoc namespaces) {
139         String JavaDoc[] namespace = namespaces.split(" ");
140
141         String JavaDoc 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     /**
153      * Add namespaces
154      */

155     private String JavaDoc addNamespaces(String JavaDoc namespaces, String JavaDoc content) {
156         int i = content.indexOf(">");
157         return content.substring(0, i) + " " + namespaces + content.substring(i);
158     }
159 }
160
Popular Tags