1/*
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3 */
4/*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements.  See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License.  You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package com.sun.org.apache.xerces.internal.dom;
22
23import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
24import com.sun.org.apache.xerces.internal.impl.xs.AttributePSVImpl;
25import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
26import com.sun.org.apache.xerces.internal.xs.*;
27import com.sun.org.apache.xerces.internal.xs.AttributePSVI;
28import java.io.IOException;
29import java.io.NotSerializableException;
30import java.io.ObjectInputStream;
31import java.io.ObjectOutputStream;
32
33/**
34 * Attribute namespace implementation; stores PSVI attribute items.
35 *
36 * @xerces.internal
37 *
38 * @author Sandy Gao, IBM
39 *
40 */
41public class PSVIAttrNSImpl extends AttrNSImpl implements AttributePSVI {
42
43    /** Serialization version. */
44    static final long serialVersionUID = -3241738699421018889L;
45
46    /**
47     * Construct an attribute node.
48     */
49    public PSVIAttrNSImpl(CoreDocumentImpl ownerDocument, String namespaceURI,
50                          String qualifiedName, String localName) {
51        super(ownerDocument, namespaceURI, qualifiedName, localName);
52    }
53
54    /**
55     * Construct an attribute node.
56     */
57    public PSVIAttrNSImpl(CoreDocumentImpl ownerDocument, String namespaceURI,
58                          String qualifiedName) {
59        super(ownerDocument, namespaceURI, qualifiedName);
60    }
61
62    /** attribute declaration */
63    protected XSAttributeDeclaration fDeclaration = null;
64
65    /** type of attribute, simpleType */
66    protected XSTypeDefinition fTypeDecl = null;
67
68    /** If this attribute was explicitly given a
69     * value in the original document, this is true; otherwise, it is false  */
70    protected boolean fSpecified = true;
71
72    /** Schema value */
73    protected ValidatedInfo fValue = new ValidatedInfo();
74
75    /** validation attempted: none, partial, full */
76    protected short fValidationAttempted = AttributePSVI.VALIDATION_NONE;
77
78    /** validity: valid, invalid, unknown */
79    protected short fValidity = AttributePSVI.VALIDITY_NOTKNOWN;
80
81    /** error codes */
82    protected StringList fErrorCodes = null;
83
84    /** error messages */
85    protected StringList fErrorMessages = null;
86
87    /** validation context: could be QName or XPath expression*/
88    protected String fValidationContext = null;
89
90    //
91    // AttributePSVI methods
92    //
93
94    /* (non-Javadoc)
95     * @see org.apache.xerces.xs.ItemPSVI#constant()
96     */
97    public ItemPSVI constant() {
98        return new AttributePSVImpl(true, this);
99    }
100
101    /* (non-Javadoc)
102     * @see org.apache.xerces.xs.ItemPSVI#isConstant()
103     */
104    public boolean isConstant() {
105        return false;
106    }
107
108    /**
109     * [schema default]
110     *
111     * @return The canonical lexical representation of the declaration's {value constraint} value.
112     * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-schema_default>XML Schema Part 1: Structures [schema default]</a>
113     */
114    @SuppressWarnings("deprecation")
115    public String getSchemaDefault() {
116        return fDeclaration == null ? null : fDeclaration.getConstraintValue();
117    }
118
119    /**
120     * [schema normalized value]
121     *
122     *
123     * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-schema_normalized_value>XML Schema Part 1: Structures [schema normalized value]</a>
124     * @return the normalized value of this item after validation
125     */
126    @Deprecated
127    public String getSchemaNormalizedValue() {
128        return fValue.getNormalizedValue();
129    }
130
131    /**
132     * [schema specified]
133     * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-schema_specified">XML Schema Part 1: Structures [schema specified]</a>
134     * @return false value was specified in schema, true value comes from the infoset
135     */
136    public boolean getIsSchemaSpecified() {
137        return fSpecified;
138    }
139
140
141    /**
142     * Determines the extent to which the document has been validated
143     *
144     * @return return the [validation attempted] property. The possible values are
145     *         NO_VALIDATION, PARTIAL_VALIDATION and FULL_VALIDATION
146     */
147    public short getValidationAttempted() {
148        return fValidationAttempted;
149    }
150
151    /**
152     * Determine the validity of the node with respect
153     * to the validation being attempted
154     *
155     * @return return the [validity] property. Possible values are:
156     *         UNKNOWN_VALIDITY, INVALID_VALIDITY, VALID_VALIDITY
157     */
158    public short getValidity() {
159        return fValidity;
160    }
161
162    /**
163     * A list of error codes generated from validation attempts.
164     * Need to find all the possible subclause reports that need reporting
165     *
166     * @return list of error codes
167     */
168    public StringList getErrorCodes() {
169        if (fErrorCodes != null) {
170            return fErrorCodes;
171        }
172        return StringListImpl.EMPTY_LIST;
173    }
174
175    /**
176     * A list of error messages generated from the validation attempt or
177     * an empty <code>StringList</code> if no errors occurred during the
178     * validation attempt. The indices of error messages in this list are
179     * aligned with those in the <code>[schema error code]</code> list.
180     */
181    public StringList getErrorMessages() {
182        if (fErrorMessages != null) {
183            return fErrorMessages;
184        }
185        return StringListImpl.EMPTY_LIST;
186    }
187
188    // This is the only information we can provide in a pipeline.
189    public String getValidationContext() {
190        return fValidationContext;
191    }
192
193    /**
194     * An item isomorphic to the type definition used to validate this element.
195     *
196     * @return  a type declaration
197     */
198    public XSTypeDefinition getTypeDefinition() {
199        return fTypeDecl;
200    }
201
202    /**
203     * If and only if that type definition is a simple type definition
204     * with {variety} union, or a complex type definition whose {content type}
205     * is a simple type definition with {variety} union, then an item isomorphic
206     * to that member of the union's {member type definitions} which actually
207     * validated the element item's normalized value.
208     *
209     * @return  a simple type declaration
210     */
211    public XSSimpleTypeDefinition getMemberTypeDefinition() {
212        return fValue.getMemberTypeDefinition();
213    }
214
215    /**
216     * An item isomorphic to the attribute declaration used to validate
217     * this attribute.
218     *
219     * @return  an attribute declaration
220     */
221    public XSAttributeDeclaration getAttributeDeclaration() {
222        return fDeclaration;
223    }
224
225    /**
226     * Copy PSVI properties from another psvi item.
227     *
228     * @param attr  the source of attribute PSVI items
229     */
230    public void setPSVI(AttributePSVI attr) {
231        this.fDeclaration = attr.getAttributeDeclaration();
232        this.fValidationContext = attr.getValidationContext();
233        this.fValidity = attr.getValidity();
234        this.fValidationAttempted = attr.getValidationAttempted();
235        this.fErrorCodes = attr.getErrorCodes();
236        this.fErrorMessages = attr.getErrorMessages();
237        this.fValue.copyFrom(attr.getSchemaValue());
238        this.fTypeDecl = attr.getTypeDefinition();
239        this.fSpecified = attr.getIsSchemaSpecified();
240    }
241
242    /* (non-Javadoc)
243     * @see com.sun.org.apache.xerces.internal.xs.ItemPSVI#getActualNormalizedValue()
244     */
245    @Deprecated
246    public Object getActualNormalizedValue() {
247        return fValue.getActualValue();
248    }
249
250    /* (non-Javadoc)
251     * @see com.sun.org.apache.xerces.internal.xs.ItemPSVI#getActualNormalizedValueType()
252     */
253    @Deprecated
254    public short getActualNormalizedValueType() {
255        return fValue.getActualValueType();
256    }
257
258    /* (non-Javadoc)
259     * @see com.sun.org.apache.xerces.internal.xs.ItemPSVI#getItemValueTypes()
260     */
261    @Deprecated
262    public ShortList getItemValueTypes() {
263        return fValue.getListValueTypes();
264    }
265
266    /* (non-Javadoc)
267     * @see org.apache.xerces.xs.ItemPSVI#getSchemaValue()
268     */
269    public XSValue getSchemaValue() {
270        return fValue;
271    }
272
273    // REVISIT: Forbid serialization of PSVI DOM until
274    // we support object serialization of grammars -- mrglavas
275
276    private void writeObject(ObjectOutputStream out)
277        throws IOException {
278        throw new NotSerializableException(getClass().getName());
279    }
280
281    private void readObject(ObjectInputStream in)
282        throws IOException, ClassNotFoundException {
283        throw new NotSerializableException(getClass().getName());
284    }
285}
286