author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.xml.internal.bind.v2.runtime.unmarshaller; |
|
27 |
||
28 |
import javax.xml.namespace.NamespaceContext; |
|
29 |
import javax.xml.validation.Schema; |
|
30 |
import javax.xml.validation.ValidatorHandler; |
|
31 |
||
32 |
import com.sun.xml.internal.bind.v2.util.FatalAdapter; |
|
33 |
||
34 |
import org.xml.sax.SAXException; |
|
35 |
||
36 |
/** |
|
37 |
* {@link XmlVisitor} decorator that validates the events by using JAXP validation API. |
|
38 |
* |
|
39 |
* @author Kohsuke Kawaguchi |
|
40 |
*/ |
|
41 |
final class ValidatingUnmarshaller implements XmlVisitor, XmlVisitor.TextPredictor { |
|
42 |
||
43 |
private final XmlVisitor next; |
|
44 |
private final ValidatorHandler validator; |
|
45 |
private NamespaceContext nsContext = null; |
|
46 |
||
47 |
/** |
|
48 |
* {@link TextPredictor} of the next {@link XmlVisitor}. |
|
49 |
*/ |
|
50 |
private final TextPredictor predictor; |
|
51 |
||
52 |
private char[] buf = new char[256]; |
|
53 |
||
54 |
/** |
|
55 |
* Creates a new instance of ValidatingUnmarshaller. |
|
56 |
*/ |
|
57 |
public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) { |
|
58 |
this.validator = schema.newValidatorHandler(); |
|
59 |
this.next = next; |
|
60 |
this.predictor = next.getPredictor(); |
|
61 |
// if the user bothers to use a validator, make validation errors fatal |
|
62 |
// so that it will abort unmarshalling. |
|
63 |
validator.setErrorHandler(new FatalAdapter(getContext())); |
|
64 |
} |
|
65 |
||
66 |
public void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException { |
|
67 |
this.nsContext = nsContext; |
|
68 |
validator.setDocumentLocator(locator); |
|
69 |
validator.startDocument(); |
|
70 |
next.startDocument(locator,nsContext); |
|
71 |
} |
|
72 |
||
73 |
public void endDocument() throws SAXException { |
|
74 |
this.nsContext = null; |
|
75 |
validator.endDocument(); |
|
76 |
next.endDocument(); |
|
77 |
} |
|
78 |
||
79 |
public void startElement(TagName tagName) throws SAXException { |
|
80 |
if(nsContext != null) { |
|
81 |
String tagNamePrefix = tagName.getPrefix().intern(); |
|
82 |
if(tagNamePrefix != "") { |
|
83 |
validator.startPrefixMapping(tagNamePrefix, nsContext.getNamespaceURI(tagNamePrefix)); |
|
84 |
} |
|
85 |
} |
|
86 |
validator.startElement(tagName.uri,tagName.local,tagName.getQname(),tagName.atts); |
|
87 |
next.startElement(tagName); |
|
88 |
} |
|
89 |
||
90 |
public void endElement(TagName tagName ) throws SAXException { |
|
91 |
validator.endElement(tagName.uri,tagName.local,tagName.getQname()); |
|
92 |
next.endElement(tagName); |
|
93 |
} |
|
94 |
||
95 |
public void startPrefixMapping(String prefix, String nsUri) throws SAXException { |
|
96 |
validator.startPrefixMapping(prefix,nsUri); |
|
97 |
next.startPrefixMapping(prefix,nsUri); |
|
98 |
} |
|
99 |
||
100 |
public void endPrefixMapping(String prefix) throws SAXException { |
|
101 |
validator.endPrefixMapping(prefix); |
|
102 |
next.endPrefixMapping(prefix); |
|
103 |
} |
|
104 |
||
105 |
public void text( CharSequence pcdata ) throws SAXException { |
|
106 |
int len = pcdata.length(); |
|
107 |
if(buf.length<len) { |
|
108 |
buf = new char[len]; |
|
109 |
} |
|
110 |
for( int i=0;i<len; i++ ) |
|
111 |
buf[i] = pcdata.charAt(i); // isn't this kinda slow? |
|
112 |
||
113 |
validator.characters(buf,0,len); |
|
114 |
if(predictor.expectText()) |
|
115 |
next.text(pcdata); |
|
116 |
} |
|
117 |
||
118 |
public UnmarshallingContext getContext() { |
|
119 |
return next.getContext(); |
|
120 |
} |
|
121 |
||
122 |
public TextPredictor getPredictor() { |
|
123 |
return this; |
|
124 |
} |
|
125 |
||
126 |
// should be always invoked through TextPredictor |
|
127 |
@Deprecated |
|
128 |
public boolean expectText() { |
|
129 |
// validator needs to make sure that there's no text |
|
130 |
// even when it's not expected. So always have them |
|
131 |
// send text, ignoring optimization hints from the unmarshaller |
|
132 |
return true; |
|
133 |
} |
|
134 |
} |