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: package org.vfny.geoserver.form.data;
006:
007: import org.apache.struts.action.ActionError;
008: import org.apache.struts.action.ActionErrors;
009: import org.apache.struts.action.ActionForm;
010: import org.apache.struts.action.ActionMapping;
011: import org.vfny.geoserver.config.DataConfig;
012: import org.vfny.geoserver.config.NameSpaceConfig;
013: import org.vfny.geoserver.util.Requests;
014: import java.net.MalformedURLException;
015: import java.net.URL;
016: import java.util.regex.Pattern;
017: import javax.servlet.ServletContext;
018: import javax.servlet.http.HttpServletRequest;
019:
020: /**
021: * DOCUMENT ME!
022: *
023: * @author rgould To change the template for this generated type comment go to
024: * Window>Preferences>Java>Code Generation>Code and Comments
025: */
026: public class DataNamespacesEditorForm extends ActionForm {
027: private String URI;
028: private boolean _default;
029: private String prefix;
030: private HttpServletRequest request;
031:
032: /*
033: * Because of the way that STRUTS works, if the user does not check the default box,
034: * or unchecks it, set_default() is never called, thus we must monitor set_default()
035: * to see if it doesn't get called. This must be accessible, as ActionForms need to
036: * know about it -- there is no way we can tell whether we are about to be passed to
037: * an ActionForm or not.
038: *
039: * Probably a better way to do this, but I can't think of one.
040: * -rgould
041: */
042: private boolean defaultChecked = false;
043:
044: public void reset(ActionMapping arg0, HttpServletRequest request) {
045: super .reset(arg0, request);
046: this .request = request;
047:
048: defaultChecked = false;
049:
050: ServletContext context = getServlet().getServletContext();
051: DataConfig config = (DataConfig) context
052: .getAttribute(DataConfig.CONFIG_KEY);
053:
054: NameSpaceConfig nsConfig;
055:
056: nsConfig = Requests.getUserContainer(request)
057: .getNamespaceConfig();
058:
059: _default = nsConfig.isDefault();
060: prefix = nsConfig.getPrefix();
061: URI = nsConfig.getUri();
062: }
063:
064: public ActionErrors validate(ActionMapping mapping,
065: HttpServletRequest request) {
066: ActionErrors errors = new ActionErrors();
067:
068: if ((prefix == null) || prefix.equals("")) {
069: errors.add("prefix", new ActionError(
070: "error.prefix.required", getPrefix()));
071: } else if (!Pattern.matches("^\\w*$", prefix)) {
072: errors.add("dataStoreID", new ActionError(
073: "error.prefix.invalid", prefix));
074: }
075:
076: if ((URI == null) || URI.equals("")) {
077: errors.add("URI", new ActionError("error.uri.required",
078: prefix));
079: } else {
080: try {
081: URL url = new URL(URI);
082: } catch (MalformedURLException badURI) {
083: errors.add("dataStoreID", new ActionError(
084: "error.uri.malformed", badURI));
085: }
086: }
087:
088: return errors;
089: }
090:
091: /**
092: * DOCUMENT ME!
093: *
094: * @return
095: */
096: public boolean is_default() {
097: return _default;
098: }
099:
100: /**
101: * DOCUMENT ME!
102: *
103: * @return
104: */
105: public String getPrefix() {
106: return prefix;
107: }
108:
109: /**
110: * DOCUMENT ME!
111: *
112: * @return
113: */
114: public String getURI() {
115: return URI;
116: }
117:
118: /**
119: * DOCUMENT ME!
120: *
121: * @param b
122: */
123: public void set_default(boolean b) {
124: defaultChecked = true;
125: _default = b;
126: }
127:
128: /**
129: * DOCUMENT ME!
130: *
131: * @param string
132: */
133: public void setPrefix(String string) {
134: prefix = string;
135: }
136:
137: /**
138: * DOCUMENT ME!
139: *
140: * @param string
141: */
142: public void setURI(String string) {
143: URI = string;
144: }
145:
146: /**
147: * DOCUMENT ME!
148: *
149: * @return
150: */
151: public boolean isDefaultChecked() {
152: return defaultChecked;
153: }
154:
155: /**
156: * DOCUMENT ME!
157: *
158: * @param b
159: */
160: public void setDefaultChecked(boolean b) {
161: defaultChecked = b;
162: }
163: }
|