1 /*
2 * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
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 package org.opensaml.xml.schema.validator;
18
19 import org.opensaml.xml.schema.XSString;
20 import org.opensaml.xml.util.DatatypeHelper;
21 import org.opensaml.xml.validation.ValidationException;
22 import org.opensaml.xml.validation.Validator;
23
24 /**
25 * Checks {@link org.opensaml.xml.schema.XSString} for Schema compliance.
26 *
27 * @param <T> the type to be validated
28 */
29 public class XSStringSchemaValidator<T extends XSString> implements Validator<T> {
30
31 /** Flag specifying whether empty element content should be allowed. */
32 private boolean allowEmptyContent;
33
34 /**
35 * Constructor.
36 *
37 * @param allowEmptyElementContent flag indicated whether empty content should be allowed
38 */
39 public XSStringSchemaValidator(boolean allowEmptyElementContent) {
40 allowEmptyContent = allowEmptyElementContent;
41 }
42
43 /**
44 * Constructor.
45 *
46 * Empty content is not allowed.
47 *
48 */
49 public XSStringSchemaValidator() {
50 allowEmptyContent = false;
51 }
52
53 /** {@inheritDoc} */
54 public void validate(T xmlObject) throws ValidationException {
55 validateStringContent(xmlObject);
56 }
57
58 /**
59 * Get the flag which determines whether empty content should be allowed.
60 *
61 * @return true if empty content should be allowed, false otherwise
62 */
63 protected boolean isAllowEmptyContent() {
64 return allowEmptyContent;
65 }
66
67 /**
68 * Validates the content of the XSBase64Binary object.
69 *
70 * @param xmlObject the object to evaluate
71 * @throws ValidationException thrown if the content of the Base64Binary object is invalid
72 */
73 protected void validateStringContent(T xmlObject) throws ValidationException {
74 if (! isAllowEmptyContent()) {
75 if (DatatypeHelper.isEmpty(xmlObject.getValue())) {
76 throw new ValidationException("String content may not be empty");
77 }
78 }
79 }
80
81 }