1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xerces.internal.impl.xs;
23
24import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
25import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
26import com.sun.org.apache.xerces.internal.xs.ElementPSVI;
27import com.sun.org.apache.xerces.internal.xs.ItemPSVI;
28import com.sun.org.apache.xerces.internal.xs.ShortList;
29import com.sun.org.apache.xerces.internal.xs.StringList;
30import com.sun.org.apache.xerces.internal.xs.XSConstants;
31import com.sun.org.apache.xerces.internal.xs.XSElementDeclaration;
32import com.sun.org.apache.xerces.internal.xs.XSModel;
33import com.sun.org.apache.xerces.internal.xs.XSNotationDeclaration;
34import com.sun.org.apache.xerces.internal.xs.XSSimpleTypeDefinition;
35import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
36import com.sun.org.apache.xerces.internal.xs.XSValue;
37
38/**
39 * Element PSV infoset augmentations implementation.
40 * The following information will be available at the startElement call:
41 * name, namespace, type, notation, validation context
42 *
43 * The following information will be available at the endElement call:
44 * nil, specified, normalized value, member type, validity, error codes,
45 * default
46 *
47 * @xerces.internal
48 *
49 * @author Elena Litani IBM
50 */
51public class ElementPSVImpl implements ElementPSVI {
52
53    /** element declaration */
54    protected XSElementDeclaration fDeclaration = null;
55
56    /** type of element, could be xsi:type */
57    protected XSTypeDefinition fTypeDecl = null;
58
59    /** true if clause 3.2 of Element Locally Valid (Element) (3.3.4)
60      * is satisfied, otherwise false
61      */
62    protected boolean fNil = false;
63
64    /** true if the element value was provided by the schema; false otherwise.
65     */
66    protected boolean fSpecified = false;
67
68    /** Schema value */
69    protected ValidatedInfo fValue = new ValidatedInfo();
70
71    /** http://www.w3.org/TR/xmlschema-1/#e-notation*/
72    protected XSNotationDeclaration fNotation = null;
73
74    /** validation attempted: none, partial, full */
75    protected short fValidationAttempted = ElementPSVI.VALIDATION_NONE;
76
77    /** validity: valid, invalid, unknown */
78    protected short fValidity = ElementPSVI.VALIDITY_NOTKNOWN;
79
80    /** error codes and error messages */
81    protected String[] fErrors = null;
82
83    /** validation context: could be QName or XPath expression*/
84    protected String fValidationContext = null;
85
86    /** deferred XSModel **/
87    protected SchemaGrammar[] fGrammars = null;
88
89    /** the schema information property */
90    protected XSModel fSchemaInformation = null;
91
92    /** true if this object is immutable **/
93    protected boolean fIsConstant;
94
95    public ElementPSVImpl() {}
96
97    public ElementPSVImpl(boolean isConstant, ElementPSVI elementPSVI) {
98        fDeclaration = elementPSVI.getElementDeclaration();
99        fTypeDecl = elementPSVI.getTypeDefinition();
100        fNil = elementPSVI.getNil();
101        fSpecified = elementPSVI.getIsSchemaSpecified();
102        fValue.copyFrom(elementPSVI.getSchemaValue());
103        fNotation = elementPSVI.getNotation();
104        fValidationAttempted = elementPSVI.getValidationAttempted();
105        fValidity = elementPSVI.getValidity();
106        fValidationContext = elementPSVI.getValidationContext();
107        if (elementPSVI instanceof ElementPSVImpl) {
108            final ElementPSVImpl elementPSVIImpl = (ElementPSVImpl) elementPSVI;
109            fErrors = (elementPSVIImpl.fErrors != null) ?
110                    (String[]) elementPSVIImpl.fErrors.clone() : null;
111            elementPSVIImpl.copySchemaInformationTo(this);
112        }
113        else {
114            final StringList errorCodes = elementPSVI.getErrorCodes();
115            final int length = errorCodes.getLength();
116            if (length > 0) {
117                final StringList errorMessages = elementPSVI.getErrorMessages();
118                final String[] errors = new String[length << 1];
119                for (int i = 0, j = 0; i < length; ++i) {
120                    errors[j++] = errorCodes.item(i);
121                    errors[j++] = errorMessages.item(i);
122                }
123                fErrors = errors;
124            }
125            fSchemaInformation = elementPSVI.getSchemaInformation();
126        }
127        fIsConstant = isConstant;
128    }
129
130    //
131    // ElementPSVI methods
132    //
133
134    /* (non-Javadoc)
135     * @see org.apache.xerces.xs.ItemPSVI#constant()
136     */
137    public ItemPSVI constant() {
138        if (isConstant()) {
139            return this;
140        }
141        return new ElementPSVImpl(true, this);
142    }
143
144    /* (non-Javadoc)
145     * @see org.apache.xerces.xs.ItemPSVI#isConstant()
146     */
147    public boolean isConstant() {
148        return fIsConstant;
149    }
150
151    /**
152     * [schema default]
153     *
154     * @return The canonical lexical representation of the declaration's {value constraint} value.
155     * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-schema_default>XML Schema Part 1: Structures [schema default]</a>
156     */
157    public String getSchemaDefault() {
158        return fDeclaration == null ? null : fDeclaration.getConstraintValue();
159    }
160
161    /**
162     * [schema normalized value]
163     *
164     *
165     * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-schema_normalized_value>XML Schema Part 1: Structures [schema normalized value]</a>
166     * @return the normalized value of this item after validation
167     */
168    public String getSchemaNormalizedValue() {
169        return fValue.getNormalizedValue();
170    }
171
172    /**
173     * [schema specified]
174     * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-schema_specified">XML Schema Part 1: Structures [schema specified]</a>
175     * @return true - value was specified in schema, false - value comes from the infoset
176     */
177    public boolean getIsSchemaSpecified() {
178        return fSpecified;
179    }
180
181    /**
182     * Determines the extent to which the document has been validated
183     *
184     * @return return the [validation attempted] property. The possible values are
185     *         NO_VALIDATION, PARTIAL_VALIDATION and FULL_VALIDATION
186     */
187    public short getValidationAttempted() {
188        return fValidationAttempted;
189    }
190
191    /**
192     * Determine the validity of the node with respect
193     * to the validation being attempted
194     *
195     * @return return the [validity] property. Possible values are:
196     *         UNKNOWN_VALIDITY, INVALID_VALIDITY, VALID_VALIDITY
197     */
198    public short getValidity() {
199        return fValidity;
200    }
201
202    /**
203     * A list of error codes generated from validation attempts.
204     * Need to find all the possible subclause reports that need reporting
205     *
206     * @return Array of error codes
207     */
208    public StringList getErrorCodes() {
209        if (fErrors == null || fErrors.length == 0) {
210            return StringListImpl.EMPTY_LIST;
211        }
212        return new PSVIErrorList(fErrors, true);
213    }
214
215    /**
216     * A list of error messages generated from the validation attempt or
217     * an empty <code>StringList</code> if no errors occurred during the
218     * validation attempt. The indices of error messages in this list are
219     * aligned with those in the <code>[schema error code]</code> list.
220     */
221    public StringList getErrorMessages() {
222        if (fErrors == null || fErrors.length == 0) {
223            return StringListImpl.EMPTY_LIST;
224        }
225        return new PSVIErrorList(fErrors, false);
226    }
227
228    // This is the only information we can provide in a pipeline.
229    public String getValidationContext() {
230        return fValidationContext;
231    }
232
233    /**
234     * [nil]
235     * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-nil>XML Schema Part 1: Structures [nil]</a>
236     * @return true if clause 3.2 of Element Locally Valid (Element) (3.3.4) above is satisfied, otherwise false
237     */
238    public boolean getNil() {
239        return fNil;
240    }
241
242    /**
243     * [notation]
244     * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-notation>XML Schema Part 1: Structures [notation]</a>
245     * @return The notation declaration.
246     */
247    public XSNotationDeclaration getNotation() {
248        return fNotation;
249    }
250
251    /**
252     * An item isomorphic to the type definition used to validate this element.
253     *
254     * @return  a type declaration
255     */
256    public XSTypeDefinition getTypeDefinition() {
257        return fTypeDecl;
258    }
259
260    /**
261     * If and only if that type definition is a simple type definition
262     * with {variety} union, or a complex type definition whose {content type}
263     * is a simple thype definition with {variety} union, then an item isomorphic
264     * to that member of the union's {member type definitions} which actually
265     * validated the element item's normalized value.
266     *
267     * @return  a simple type declaration
268     */
269    public XSSimpleTypeDefinition getMemberTypeDefinition() {
270        return fValue.getMemberTypeDefinition();
271    }
272
273    /**
274     * An item isomorphic to the element declaration used to validate
275     * this element.
276     *
277     * @return  an element declaration
278     */
279    public XSElementDeclaration getElementDeclaration() {
280        return fDeclaration;
281    }
282
283    /**
284     * [schema information]
285     * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-schema_information">XML Schema Part 1: Structures [schema information]</a>
286     * @return The schema information property if it's the validation root,
287     *         null otherwise.
288     */
289    public synchronized XSModel getSchemaInformation() {
290        if (fSchemaInformation == null && fGrammars != null) {
291            fSchemaInformation = new XSModelImpl(fGrammars);
292        }
293        return fSchemaInformation;
294    }
295
296    /* (non-Javadoc)
297     * @see com.sun.org.apache.xerces.internal.xs.ItemPSVI#getActualNormalizedValue()
298     */
299    public Object getActualNormalizedValue() {
300        return fValue.getActualValue();
301    }
302
303    /* (non-Javadoc)
304     * @see com.sun.org.apache.xerces.internal.xs.ItemPSVI#getActualNormalizedValueType()
305     */
306    public short getActualNormalizedValueType() {
307        return fValue.getActualValueType();
308    }
309
310    /* (non-Javadoc)
311     * @see com.sun.org.apache.xerces.internal.xs.ItemPSVI#getItemValueTypes()
312     */
313    public ShortList getItemValueTypes() {
314        return fValue.getListValueTypes();
315    }
316
317    /* (non-Javadoc)
318     * @see com.sun.org.apache.xerces.internal.xs.ItemPSVI#getSchemaValue()
319     */
320    public XSValue getSchemaValue() {
321        return fValue;
322    }
323
324    /**
325     * Reset() should be called in validator startElement(..) method.
326     */
327    public void reset() {
328        fDeclaration = null;
329        fTypeDecl = null;
330        fNil = false;
331        fSpecified = false;
332        fNotation = null;
333        fValidationAttempted = ElementPSVI.VALIDATION_NONE;
334        fValidity = ElementPSVI.VALIDITY_NOTKNOWN;
335        fErrors = null;
336        fValidationContext = null;
337        fValue.reset();
338    }
339
340    public void copySchemaInformationTo(ElementPSVImpl target) {
341        target.fGrammars = fGrammars;
342        target.fSchemaInformation = fSchemaInformation;
343    }
344}
345