KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > settings > HtmlToXconfReader


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.modules.settings;
14
15 import java.io.StringReader JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import com.openedit.OpenEditException;
22 import com.openedit.WebPageRequest;
23 import com.openedit.config.Configuration;
24 import com.openedit.config.XMLConfiguration;
25 import com.openedit.page.Page;
26 import com.openedit.page.XconfConfiguration;
27
28
29 /**
30  * This action saves the configuration file for the page with the path given by the
31  * <samp>path</samp> request parameter, where the configuration file's content is given by the
32  * <samp>content</samp> request parameter.
33  *
34  * @author Eric Galluzzo
35  */

36 public class HtmlToXconfReader
37 {
38     protected boolean fieldAdvancedMode;
39     
40     public void saveChangesToConfig(WebPageRequest inReq, XconfConfiguration inConfig) throws OpenEditException
41     {
42         Map JavaDoc props = inReq.getParameterMap();
43         
44         //Updated to exsting properties
45
Configuration oldeditable = inConfig.getProperty("editable");
46         inConfig.removeAllProperties();
47         
48         saveProperties(inReq, inConfig);
49
50         if ( isAdvancedMode() )
51         {
52             saveStandard(props, inConfig);
53             saveLayout(props, inConfig);
54             savePermissions( props, inConfig);
55         }
56         else
57         {
58             if ( oldeditable != null)
59             {
60                 inConfig.saveProperty("editable",oldeditable.getValue(),null);
61             }
62         }
63         //saveGenerators(props, inConfig);
64
}
65
66     protected void savePermissions(Map JavaDoc inProps, XconfConfiguration inConfig)
67     {
68         String JavaDoc view = (String JavaDoc)inProps.get("view-requirements");
69         inConfig.removeElements("view-requirements");
70         if( view != null && view.trim().length() > 0)
71         {
72             Configuration nconf = inConfig.addChild("view-requirements");
73             StringReader JavaDoc read = new StringReader JavaDoc(view);
74             nconf.addChild(new XMLConfiguration(read) );
75         }
76
77         String JavaDoc edit = (String JavaDoc)inProps.get("edit-requirements");
78         inConfig.removeElements("edit-requirements");
79         if( edit != null && edit.trim().length() > 0)
80         {
81             Configuration nconf = inConfig.addChild("edit-requirements");
82             StringReader JavaDoc read = new StringReader JavaDoc(edit);
83             nconf.addChild(new XMLConfiguration(read) );
84         }
85     }
86
87     protected void saveContentFile(Map JavaDoc inProperties, XconfConfiguration inConfig)
88     {
89         //content file
90
String JavaDoc contentfile = (String JavaDoc) inProperties.get("contentfile");
91
92         inConfig.setContentFile(contentfile);
93     }
94
95     protected void saveGenerators(Map JavaDoc inProperties, XconfConfiguration inConfig)
96     {
97         //generator
98
inConfig.removeElements("generator");
99
100         String JavaDoc generatorKey = "generator";
101
102         //the chices are velocity-xslt xslt velocity jsp
103
String JavaDoc generator = (String JavaDoc) inProperties.get(generatorKey);
104
105         if ((generator == null) || (generator.length() == 0) || "default".equals(generator))
106         {
107             return;
108         }
109         else
110         {
111             XMLConfiguration currentGen = (XMLConfiguration)inConfig.addChild("generator");
112             String JavaDoc xslt = (String JavaDoc) inProperties.get(generatorKey + ".xslt");
113
114             currentGen.setAttribute("name", generator);
115             
116             
117             //TODO: We lost support for compound genertors
118
}
119     }
120
121     protected void saveNewProperty(WebPageRequest inReq, XconfConfiguration inConfig, int inCount)
122     {
123         //We need two things, The name and the value
124
String JavaDoc name = inReq.getRequestParameter("newproperty." + inCount + ".name");
125
126         String JavaDoc value = inReq.getRequestParameter("newproperty." + inCount + ".value");
127
128         if ((value != null) && (value.length() != 0))
129         {
130             String JavaDoc locale = inReq.getRequestParameter("newproperty." + inCount + ".language");
131             inConfig.saveProperty(name, value, locale);
132         }
133         else
134         {
135             inConfig.removeProperty(name);
136         }
137     }
138
139     protected void saveProperties(WebPageRequest inReq, XconfConfiguration inConfig)
140     {
141         
142         Map JavaDoc props = inReq.getParameterMap();
143
144
145         Set JavaDoc set = props.keySet();
146         List JavaDoc keys = new ArrayList JavaDoc(set);
147 // Collections.sort(keys, new Comparator() {
148
// public int compare(Object inO1, Object inO2)
149
// {
150
// String one = (String)inO1;
151
// String two = (String)inO2;
152
// if( one.startsWith("property") && two.startsWith("property") && one.endsWith(".name") && two.endsWith(".name"))
153
// {
154
// String count1 = one.substring("property".length() + 1, one.lastIndexOf("."));
155
// String count2 = two.substring("property".length() + 1, two.lastIndexOf("."));
156
// return new Integer(count1).compareTo(new Integer(count2));
157
// }
158
// return 0;
159
// }
160
// });
161

162         for (int i = 0; i < keys.size(); i++)
163         {
164             String JavaDoc propname = inReq.getRequestParameter("property." + i + ".name");
165             if( propname != null && propname.length() > 0 )
166             {
167                 String JavaDoc valuetag = "property." + i + ".value";
168                 String JavaDoc[] values = inReq.getRequestParameters(valuetag);
169                 String JavaDoc langtag = "property." + i + ".language";
170                 String JavaDoc[] locales = inReq.getRequestParameters(langtag);
171                 
172                 if ((values != null) && (values.length > 0))
173                 {
174                     for (int v = 0; v < values.length; v++)
175                     {
176                         String JavaDoc value = values[v];
177                         if( value != null && value.length() > 0)
178                         {
179                             if( value.equals("on"))
180                             {
181                                 value = "true";
182                                 
183                             }
184                             String JavaDoc locale = locales[v];
185                             inConfig.saveProperty(propname, value, locale);
186                         }
187                     }
188                 }
189             }
190         }
191         //Now loop over new properties looking for any properties that start with newprop..
192
saveNewProperty(inReq, inConfig, 1);
193         saveNewProperty(inReq, inConfig, 2);
194     }
195
196     protected void saveLayout(Map JavaDoc inProperties, XconfConfiguration inConfig)
197     {
198         //Save the template
199
String JavaDoc layout = (String JavaDoc) inProperties.get("layout");
200         inConfig.removeConfigurations("layout");
201
202         if ( layout != null && layout.equals(Page.BLANK_LAYOUT))
203         {
204             inConfig.addChild("layout");
205         }
206         else if (layout != null && layout.length() > 0)
207         {
208             inConfig.addChild("layout").setValue(layout);
209         }
210         
211         String JavaDoc innerlayout = (String JavaDoc) inProperties.get("inner-layout");
212         inConfig.removeConfigurations("inner-layout");
213         if ( innerlayout != null && innerlayout.equals(Page.BLANK_LAYOUT))
214         {
215             inConfig.addChild("inner-layout");
216         }
217         else if (innerlayout != null && innerlayout.length() > 0)
218         {
219             inConfig.addChild("inner-layout").setValue(innerlayout);
220         }
221     }
222     
223     protected void saveStandard(Map JavaDoc inProperties, XconfConfiguration inConfig)
224     {
225         String JavaDoc editable = (String JavaDoc) inProperties.get("editable");
226         if ( editable != null && editable.length() > 0)
227         {
228             if( editable.equals("ok"))
229             {
230                 editable = "true";
231             }
232             inConfig.saveProperty("editable",editable,null);
233         }
234         
235     }
236
237     public boolean isAdvancedMode()
238     {
239         return fieldAdvancedMode;
240     }
241
242     public void setAdvancedMode(boolean inAdvancedMode)
243     {
244         fieldAdvancedMode = inAdvancedMode;
245     }
246
247 }
248
Popular Tags