001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/shared/control/XmlValidator.java $
003: * $Id: XmlValidator.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.shared.control;
021:
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.sakaiproject.component.cover.ComponentManager;
028: import org.sakaiproject.metaobj.shared.mgt.StructuredArtifactValidationService;
029: import org.sakaiproject.metaobj.shared.model.ElementBean;
030: import org.sakaiproject.metaobj.shared.model.ValidationError;
031: import org.sakaiproject.metaobj.utils.mvc.impl.ValidatorBase;
032: import org.springframework.validation.Errors;
033:
034: public class XmlValidator extends ValidatorBase {
035: protected final Log logger = LogFactory.getLog(getClass());
036: protected String parentPrefix = "";
037:
038: //private FileNameValidator fileNameValidator;
039:
040: public XmlValidator() {
041: }
042:
043: public XmlValidator(String parentPrefix) {
044: this .parentPrefix = parentPrefix;
045: }
046:
047: public boolean supports(Class clazz) {
048: return (ElementBean.class.isAssignableFrom(clazz));
049: }
050:
051: public void validate(Object obj, Errors errors) {
052: validate(obj, errors, false);
053: }
054:
055: public void validate(Object obj, Errors errors,
056: boolean checkListNumbers) {
057: ElementBean elementBean = (ElementBean) obj;
058:
059: StructuredArtifactValidationService service = getStructuredArtifactValidationService();
060: List errorList = service.validate(elementBean);
061:
062: for (Iterator i = errorList.iterator(); i.hasNext();) {
063: ValidationError error = (ValidationError) i.next();
064: errors.rejectValue(error.getFieldName(), error
065: .getErrorCode(), error.getErrorInfo(), error
066: .getDefaultMessage());
067: }
068: }
069:
070: protected StructuredArtifactValidationService getStructuredArtifactValidationService() {
071: return (StructuredArtifactValidationService) ComponentManager
072: .getInstance().get(
073: StructuredArtifactValidationService.class);
074: }
075:
076: /*
077: protected void validateDisplayName(ElementBean elementBean, Errors errors) {
078: // don't care about display name here
079: }
080:
081: protected void validateElement(Element rootElement, SchemaNode childSchema,
082: Object value, Errors errors) {
083: validateChildElement(rootElement.getChild(childSchema.getName()),
084: childSchema, value, errors);
085: }
086:
087:
088: protected void validateChildElement(Element childElement, SchemaNode childSchema,
089: Object value, Errors errors) {
090: if (childElement != null) {
091: String stringValue = null;
092: if (value != null && value instanceof String) {
093: stringValue = (String) value;
094: value = childSchema.getActualNormalizedValue(stringValue);
095: }
096:
097: childElement.setText(childSchema.getSchemaNormalizedValue(value));
098: } else if (childSchema.getMinOccurs() > 0) {
099: errors.rejectValue(childSchema.getName(),
100: "required value {0}",
101: new Object[]{childSchema.getName()},
102: MessageFormat.format("required value {0}", new Object[]{childSchema.getName()}));
103: }
104: }
105:
106: protected boolean checkWrappedField(SchemaNode childSchema, ElementBean elementBean, Errors errors) {
107:
108: Class childClass = elementBean.getType(childSchema.getName());
109: Object value = elementBean.get(childSchema.getName());
110:
111: if (!(value instanceof FieldValueWrapper)) {
112: return false;
113: }
114:
115: FieldValueWrapper beanValue = (FieldValueWrapper) elementBean.get(childSchema.getName());
116:
117: if (beanValue.getValue() == null) {
118: if (childSchema.getMinOccurs() > 0) {
119: throw new NormalizationException("Required field", "required field {0}",
120: new Object[]{childSchema.getName()});
121: } else {
122: elementBean.getBaseElement().removeChild(childSchema.getName());
123: return true;
124: }
125: }
126:
127: // check date...
128: this.pushNestedPath(childSchema.getName(), errors);
129: beanValue.validate(errors);
130: this.popNestedPath(errors);
131:
132: Element dateElement = elementBean.getBaseElement().getChild(childSchema.getName());
133:
134: if (dateElement == null) {
135: dateElement = new Element(childSchema.getName());
136: elementBean.getBaseElement().addContent(dateElement);
137: }
138: dateElement.setText(childSchema.getSchemaNormalizedValue(beanValue.getValue()));
139:
140: return true;
141: }
142:
143: // public FileNameValidator getFileNameValidator() {
144: // return fileNameValidator;
145: // }
146: //
147: // public void setFileNameValidator(FileNameValidator fileNameValidator) {
148: // this.fileNameValidator = fileNameValidator;
149: // }
150:
151: */
152: }
|