1 /* Copyright 2004 The Apache Software Foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package org.apache.xmlbeans.impl.validator; 17 18 import org.apache.xmlbeans.SchemaType; 19 import org.apache.xmlbeans.XmlCursor; 20 import org.apache.xmlbeans.impl.common.PrefixResolver; 21 import org.apache.xmlbeans.impl.common.ValidatorListener; 22 import org.apache.xmlbeans.impl.common.XmlWhitespace; 23 24 import javax.xml.namespace.QName; 25 import java.util.Collection; 26 27 /** 28 * Author: Cezar Andrei (cezar.andrei at bea.com) 29 * Date: Feb 5, 2004 30 */ 31 public class ValidatorUtil 32 { 33 private static class EventImpl implements ValidatorListener.Event 34 { 35 PrefixResolver _prefixResolver; 36 String _text; 37 38 EventImpl(PrefixResolver prefixResolver, String text) 39 { 40 _prefixResolver = prefixResolver; 41 _text = text; 42 } 43 44 // can return null, used only to locate errors 45 public XmlCursor getLocationAsCursor() 46 { 47 return null; 48 } 49 50 public javax.xml.stream.Location getLocation() 51 { 52 return null; 53 } 54 55 // fill up chars with the xsi:type attribute value if there is one othervise return false 56 public String getXsiType() // BEGIN xsi:type 57 { 58 return null; 59 } 60 61 // fill up chars with xsi:nill attribute value if any 62 public String getXsiNil() // BEGIN xsi:nil 63 { 64 return null; 65 } 66 67 public String getXsiLoc() // BEGIN xsi:schemaLocation 68 { 69 return null; 70 } 71 72 public String getXsiNoLoc() // BEGIN xsi:noNamespaceSchemaLocation 73 { 74 return null; 75 } 76 77 // On START and ATTR 78 public QName getName() 79 { 80 return null; 81 } 82 83 // On TEXT and ATTR 84 public String getText() 85 { 86 return _text; 87 } 88 89 public String getText(int wsr) 90 { 91 return XmlWhitespace.collapse( _text, wsr ); 92 } 93 94 public boolean textIsWhitespace() 95 { 96 return false; 97 } 98 99 public String getNamespaceForPrefix(String prefix) 100 { 101 return _prefixResolver.getNamespaceForPrefix(prefix); 102 } 103 } 104 105 public static boolean validateSimpleType ( 106 SchemaType type, String value, Collection errors, PrefixResolver prefixResolver ) 107 { 108 if (!type.isSimpleType() && type.getContentType() != SchemaType.SIMPLE_CONTENT) 109 { 110 assert false; 111 throw new RuntimeException( "Not a simple type" ); 112 } 113 114 Validator validator = 115 new Validator( 116 type, null, type.getTypeSystem(), null, errors); 117 118 //make only one event at the beginning and than reuse it 119 EventImpl ev = new EventImpl(prefixResolver, value); 120 121 validator.nextEvent(ValidatorListener.BEGIN, ev); 122 123 validator.nextEvent(ValidatorListener.TEXT, ev); 124 125 validator.nextEvent(ValidatorListener.END, ev); 126 127 return validator.isValid(); 128 } 129 130 // public static void main(String[] args) 131 // { 132 // String value; 133 // value = " +1.2323 "; 134 // System.out.println("float " + validateSimpleType(XmlFloat.type, value, null , null)); 135 // value = " +234 "; 136 // System.out.println("posInt " + validateSimpleType(XmlPositiveInteger.type, value, null , null)); 137 // value = "2001-01-01"; 138 // System.out.println("IntOrDateUnion " + validateSimpleType(DocDocument.Doc.IntOrDateUnion.type, value, null , null)); 139 // value = "232321"; 140 // System.out.println("IntOrDateUnion " + validateSimpleType(DocDocument.Doc.IntOrDateUnion.type, value, null , null)); 141 // } 142 }