001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005:
006: /*
007: * Created on Jan 8, 2004
008: *
009: * To change the template for this generated file go to
010: * Window>Preferences>Java>Code Generation>Code and Comments
011: */
012: package org.vfny.geoserver.form.data;
013:
014: import org.apache.struts.action.ActionError;
015: import org.apache.struts.action.ActionErrors;
016: import org.apache.struts.action.ActionForm;
017: import org.apache.struts.action.ActionMapping;
018: import org.apache.struts.config.ControllerConfig;
019: import org.apache.struts.upload.CommonsMultipartRequestHandler;
020: import org.apache.struts.upload.FormFile;
021: import org.apache.struts.upload.MultipartRequestHandler;
022: import org.vfny.geoserver.config.ConfigRequests;
023: import org.vfny.geoserver.config.DataConfig;
024: import org.vfny.geoserver.config.StyleConfig;
025: import org.vfny.geoserver.global.GeoserverDataDirectory;
026: import org.vfny.geoserver.global.UserContainer;
027: import org.vfny.geoserver.util.Requests;
028: import org.xml.sax.SAXException;
029: import java.io.BufferedReader;
030: import java.io.File;
031: import java.io.FileReader;
032: import java.io.IOException;
033: import java.io.Reader;
034: import java.util.regex.Pattern;
035: import javax.servlet.http.HttpServletRequest;
036:
037: /**
038: * DOCUMENT ME!
039: *
040: * @author rgould To change the template for this generated type comment go to
041: * Window>Preferences>Java>Code Generation>Code and Comments
042: */
043: public class StylesEditorForm extends ActionForm {
044: private String styleID;
045: private String filename; // kept, but not used
046: private FormFile sldFile = null;
047: private boolean _default;
048: private boolean defaultChecked = false;
049: private boolean defaultInitial;
050: private String[] validationReport = null; // the SLD file with validation errors for it (saxexceptions)
051: private String sldContents;
052: private String action;
053:
054: /**
055: * okay this is a bit weird because of how struts html:checkbox works.
056: * 1. if the "thing" is check, then the set method will be called (with "true")
057: * 2. if the "thing" is not checked, then nothing happens!
058: *
059: * So, struts says to always set the thing to "false" in the reset method.
060: * That way, if there's no event (ie. its unset), its state is false
061: * If there is an event, it'll be set to true.
062: *
063: * Unforunately, this doesnt work well when you want a default value (the above give you a default value of false).
064: * To set the default to "true", then we need two variables.
065: * The main one, which you set to the default value.
066: * A secondary one that tells you if the user actually checked it.
067: *
068: * In this way, the default value will be sent to the user, but they can uncheck it!
069: */
070: private boolean fullyValidate;
071: private boolean fullyValidateChecked;
072: private String originalStyleId;
073:
074: public void reset(ActionMapping arg0, HttpServletRequest request) {
075: validationReport = null;
076: super .reset(arg0, request);
077:
078: DataConfig config = ConfigRequests.getDataConfig(request);
079:
080: UserContainer user = Requests.getUserContainer(request);
081: StyleConfig style = user.getStyle();
082: fullyValidate = true; //default value
083: fullyValidateChecked = false; //required by html:checkbox
084:
085: if (style == null) {
086: // Should not happen (unless they bookmark)
087: styleID = "";
088: originalStyleId = "";
089: _default = config.getStyles().isEmpty();
090: filename = "";
091: sldFile = null;
092: } else {
093: styleID = style.getId();
094: originalStyleId = style.getId();
095: _default = style.isDefault();
096:
097: if (style.getFilename() != null) {
098: filename = style.getFilename().getName();
099: }
100:
101: sldFile = null;
102: }
103:
104: sldContents = readSldFileContents(filename);
105:
106: defaultChecked = false;
107: defaultInitial = _default;
108: }
109:
110: public ActionErrors validate(ActionMapping mapping,
111: HttpServletRequest request) {
112: ActionErrors errors = new ActionErrors();
113:
114: if ((styleID == null) || styleID.equals("")) {
115: errors.add("styleID", new ActionError(
116: "error.styleID.required", styleID));
117:
118: return errors;
119: }
120:
121: if (!Pattern.matches("^[-\\w.:]*$", styleID)) {
122: errors.add("styleID", new ActionError(
123: "error.styleID.invalid", styleID));
124:
125: return errors;
126: }
127:
128: Boolean maxSize = (Boolean) request
129: .getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
130:
131: if ((maxSize != null) && (maxSize.booleanValue())) {
132: String size = null;
133: ControllerConfig cc = mapping.getModuleConfig()
134: .getControllerConfig();
135:
136: if (cc == null) {
137: size = Long
138: .toString(CommonsMultipartRequestHandler.DEFAULT_SIZE_MAX);
139: } else {
140: size = cc.getMaxFileSize(); // struts-config : <controller maxFileSize="nK" />
141: }
142:
143: errors.add("styleID", new ActionError(
144: "error.file.maxLengthExceeded", size));
145:
146: return errors;
147: }
148:
149: // if (this.getSldFile().getFileSize() == 0) { // filename not filed or file does not exist
150: // errors.add("styleID", new ActionError("error.file.required"));
151: //
152: // return errors;
153: // }
154: filename = this .getSldFile().getFileName();
155:
156: //Requests.getApplicationState(request);
157: return errors;
158: }
159:
160: /**
161: * Access _default property.
162: *
163: * @return Returns the _default.
164: */
165: public boolean isDefault() {
166: return _default;
167: }
168:
169: /**
170: * Set _default to _default.
171: *
172: * @param _default The _default to set.
173: */
174: public void setDefault(boolean _default) {
175: defaultChecked = true;
176: this ._default = _default;
177: }
178:
179: /**
180: * Access filename property.
181: *
182: * @return Returns the filename.
183: */
184: public String getFilename() {
185: return filename;
186: }
187:
188: /**
189: * Set filename to filename.
190: *
191: * @param filename The filename to set.
192: */
193: public void setFilename(String filename) {
194: this .filename = filename;
195: }
196:
197: /**
198: * Access formfile property.
199: *
200: * @return Returns the formfile.
201: */
202: public FormFile getSldFile() {
203: return this .sldFile;
204: }
205:
206: /**
207: * Set formfile to sldFile.
208: *
209: * @param filename The formfile to set.
210: */
211: public void setSldFile(FormFile filename) {
212: this .sldFile = filename;
213: }
214:
215: public boolean getFullyValidateChecked() {
216: return fullyValidateChecked;
217: }
218:
219: public void setFullyValidateChecked(boolean fullyValidateChecked) {
220: this .fullyValidateChecked = fullyValidateChecked;
221: }
222:
223: /**
224: * Access fullyValidate property.
225: *
226: * true -> validate against the xsd schema
227: *
228: * @return Returns the fullyValidate.
229: */
230: public boolean getFullyValidate() {
231: return fullyValidate;
232: }
233:
234: /**
235: * Set fullyValidate to fullyValidate.
236: *
237: * @param fullyValidate The fullyValidate to set.
238: */
239: public void setFullyValidate(boolean fullyValidate) {
240: fullyValidateChecked = true;
241: this .fullyValidate = fullyValidate;
242: }
243:
244: /**
245: * Access styleID property.
246: *
247: * @return Returns the styleID.
248: */
249: public String getStyleID() {
250: return styleID;
251: }
252:
253: /**
254: * Set styleID to styleID.
255: *
256: * @param styleID The styleID to set.
257: */
258: public void setStyleID(String styleID) {
259: this .styleID = styleID;
260: }
261:
262: /**
263: * Does the magic with _default & defaultChecked.
264: * <p>
265: * Because of the way that STRUTS works, if the user does not check the default box,
266: * or unchecks it, setDefault() is never called, thus we must monitor setDefault()
267: * to see if it doesn't get called. This must be accessible, as ActionForms need to
268: * know about it -- there is no way we can tell whether we are about to be passed to
269: * an ActionForm or not.
270: * </p>
271: * @return true if default shoudl be selected
272: */
273: public boolean isDefaultValue() {
274: if (defaultChecked) {
275: return _default;
276: }
277:
278: return defaultInitial;
279: }
280:
281: public String getValidationReportIndexed(int i) {
282: return validationReport[i];
283: }
284:
285: public String[] getValidationReport() {
286: return validationReport;
287: }
288:
289: public void setValidationReport(String[] exs) {
290: validationReport = exs;
291: }
292:
293: public String readSldFileContents(String sldFileName) {
294: if (sldFileName == null) {
295: return "";
296: }
297:
298: BufferedReader br = null;
299:
300: try {
301: File styleDir = new File(GeoserverDataDirectory
302: .getGeoserverDataDirectory(), "styles");
303: File styleFile = new File(styleDir, sldFileName);
304: br = new BufferedReader(new FileReader(styleFile));
305:
306: StringBuffer sb = new StringBuffer();
307: String s;
308:
309: while ((s = br.readLine()) != null)
310: sb.append(s.replaceAll("\t", " ")).append("\n");
311:
312: br.close();
313:
314: return sb.toString();
315: } catch (Throwable t) {
316: // could not read the file contents
317: if (br != null) {
318: try {
319: br.close();
320: } catch (IOException e) {
321: }
322: }
323:
324: return "-";
325: }
326: }
327:
328: public String getSldContents() {
329: return sldContents;
330: }
331:
332: public void setSldContents(String sldContents) {
333: this .sldContents = sldContents;
334: }
335:
336: public String getAction() {
337: return action;
338: }
339:
340: public void setAction(String action) {
341: this .action = action;
342: }
343:
344: public String getOriginalStyleId() {
345: return originalStyleId;
346: }
347: }
|