1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * @LastModified: Nov 2017
   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 
  22 package com.sun.org.apache.xerces.internal.impl.xs;
  23 
  24 import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
  25 import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
  26 import com.sun.org.apache.xerces.internal.xs.AttributePSVI;
  27 import com.sun.org.apache.xerces.internal.xs.ItemPSVI;
  28 import com.sun.org.apache.xerces.internal.xs.ShortList;
  29 import com.sun.org.apache.xerces.internal.xs.StringList;
  30 import com.sun.org.apache.xerces.internal.xs.XSAttributeDeclaration;
  31 import com.sun.org.apache.xerces.internal.xs.XSConstants;
  32 import com.sun.org.apache.xerces.internal.xs.XSSimpleTypeDefinition;
  33 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  34 import com.sun.org.apache.xerces.internal.xs.XSValue;
  35 
  36 /**
  37  * Attribute PSV infoset augmentations implementation.
  38  * The PSVI information for attributes will be available at the startElement call.
  39  *
  40  * @xerces.internal
  41  *
  42  * @author Elena Litani IBM
  43  */
  44 public class AttributePSVImpl implements AttributePSVI {
  45 
  46     /** attribute declaration */
  47     protected XSAttributeDeclaration fDeclaration = null;
  48 
  49     /** type of attribute, simpleType */
  50     protected XSTypeDefinition fTypeDecl = null;
  51 
  52     /** If this attribute was explicitly given a
  53      * value in the original document, this is false; otherwise, it is true */
  54     protected boolean fSpecified = false;
  55 
  56     /** Schema value */
  57     protected ValidatedInfo fValue = new ValidatedInfo();
  58 
  59     /** validation attempted: none, partial, full */
  60     protected short fValidationAttempted = AttributePSVI.VALIDATION_NONE;
  61 
  62     /** validity: valid, invalid, unknown */
  63     protected short fValidity = AttributePSVI.VALIDITY_NOTKNOWN;
  64 
  65     /** error codes and error messages */
  66     protected String[] fErrors = null;
  67 
  68     /** validation context: could be QName or XPath expression*/
  69     protected String fValidationContext = null;
  70 
  71     /** true if this object is immutable **/
  72     protected boolean fIsConstant;
  73 
  74     public AttributePSVImpl() {}
  75 
  76     public AttributePSVImpl(boolean isConstant, AttributePSVI attrPSVI) {
  77         fDeclaration = attrPSVI.getAttributeDeclaration();
  78         fTypeDecl = attrPSVI.getTypeDefinition();
  79         fSpecified = attrPSVI.getIsSchemaSpecified();
  80         fValue.copyFrom(attrPSVI.getSchemaValue());
  81         fValidationAttempted = attrPSVI.getValidationAttempted();
  82         fValidity = attrPSVI.getValidity();
  83         if (attrPSVI instanceof AttributePSVImpl) {
  84             final AttributePSVImpl attrPSVIImpl = (AttributePSVImpl) attrPSVI;
  85             fErrors = (attrPSVIImpl.fErrors != null) ? attrPSVIImpl.fErrors.clone() : null;
  86         }
  87         else {
  88             final StringList errorCodes = attrPSVI.getErrorCodes();
  89             final int length = errorCodes.getLength();
  90             if (length > 0) {
  91                 final StringList errorMessages = attrPSVI.getErrorMessages();
  92                 final String[] errors = new String[length << 1];
  93                 for (int i = 0, j = 0; i < length; ++i) {
  94                     errors[j++] = errorCodes.item(i);
  95                     errors[j++] = errorMessages.item(i);
  96                 }
  97                 fErrors = errors;
  98             }
  99         }
 100         fValidationContext = attrPSVI.getValidationContext();
 101         fIsConstant = isConstant;
 102     }
 103 
 104     //
 105     // AttributePSVI methods
 106     //
 107 
 108     /* (non-Javadoc)
 109      * @see com.sun.org.apache.xerces.internal.xs.ItemPSVI#constant()
 110      */
 111     public ItemPSVI constant() {
 112         if (isConstant()) {
 113             return this;
 114         }
 115         return new AttributePSVImpl(true, this);
 116     }
 117 
 118     /* (non-Javadoc)
 119      * @see com.sun.org.apache.xerces.internal.xs.ItemPSVI#isConstant()
 120      */
 121     public boolean isConstant() {
 122         return fIsConstant;
 123     }
 124 
 125     /**
 126      * [schema default]
 127      *
 128      * @return The canonical lexical representation of the declaration's {value constraint} value.
 129      * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-schema_default>XML Schema Part 1: Structures [schema default]</a>
 130      */
 131     @SuppressWarnings("deprecation")
 132     public String getSchemaDefault() {
 133         return fDeclaration == null ? null : fDeclaration.getConstraintValue();
 134     }
 135 
 136     /**
 137      * [schema normalized value]
 138      *
 139      *
 140      * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-schema_normalized_value>XML Schema Part 1: Structures [schema normalized value]</a>
 141      * @return the normalized value of this item after validation
 142      */
 143     @Deprecated
 144     public String getSchemaNormalizedValue() {
 145         return fValue.getNormalizedValue();
 146     }
 147 
 148     /**
 149      * [schema specified]
 150      * @see <a href="http://www.w3.org/TR/xmlschema-1/#e-schema_specified">XML Schema Part 1: Structures [schema specified]</a>
 151      * @return true - value was specified in schema, false - value comes from the infoset
 152      */
 153     public boolean getIsSchemaSpecified() {
 154         return fSpecified;
 155     }
 156 
 157 
 158     /**
 159      * Determines the extent to which the document has been validated
 160      *
 161      * @return return the [validation attempted] property. The possible values are
 162      *         NO_VALIDATION, PARTIAL_VALIDATION and FULL_VALIDATION
 163      */
 164     public short getValidationAttempted() {
 165         return fValidationAttempted;
 166     }
 167 
 168     /**
 169      * Determine the validity of the node with respect
 170      * to the validation being attempted
 171      *
 172      * @return return the [validity] property. Possible values are:
 173      *         UNKNOWN_VALIDITY, INVALID_VALIDITY, VALID_VALIDITY
 174      */
 175     public short getValidity() {
 176         return fValidity;
 177     }
 178 
 179     /**
 180      * A list of error codes generated from validation attempts.
 181      * Need to find all the possible subclause reports that need reporting
 182      *
 183      * @return list of error codes
 184      */
 185     public StringList getErrorCodes() {
 186         if (fErrors == null || fErrors.length == 0) {
 187             return StringListImpl.EMPTY_LIST;
 188         }
 189         return new PSVIErrorList(fErrors, true);
 190     }
 191 
 192     /**
 193      * A list of error messages generated from the validation attempt or
 194      * an empty <code>StringList</code> if no errors occurred during the
 195      * validation attempt. The indices of error messages in this list are
 196      * aligned with those in the <code>[schema error code]</code> list.
 197      */
 198     public StringList getErrorMessages() {
 199         if (fErrors == null || fErrors.length == 0) {
 200             return StringListImpl.EMPTY_LIST;
 201         }
 202         return new PSVIErrorList(fErrors, false);
 203     }
 204 
 205     // This is the only information we can provide in a pipeline.
 206     public String getValidationContext() {
 207         return fValidationContext;
 208     }
 209 
 210     /**
 211      * An item isomorphic to the type definition used to validate this element.
 212      *
 213      * @return  a type declaration
 214      */
 215     public XSTypeDefinition getTypeDefinition() {
 216         return fTypeDecl;
 217     }
 218 
 219     /**
 220      * If and only if that type definition is a simple type definition
 221      * with {variety} union, or a complex type definition whose {content type}
 222      * is a simple thype definition with {variety} union, then an item isomorphic
 223      * to that member of the union's {member type definitions} which actually
 224      * validated the element item's normalized value.
 225      *
 226      * @return  a simple type declaration
 227      */
 228     public XSSimpleTypeDefinition getMemberTypeDefinition() {
 229         return fValue.getMemberTypeDefinition();
 230     }
 231 
 232     /**
 233      * An item isomorphic to the attribute declaration used to validate
 234      * this attribute.
 235      *
 236      * @return  an attribute declaration
 237      */
 238     public XSAttributeDeclaration getAttributeDeclaration() {
 239         return fDeclaration;
 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 com.sun.org.apache.xerces.internal.xs.ItemPSVI#getSchemaValue()
 268      */
 269     public XSValue getSchemaValue() {
 270         return fValue;
 271     }
 272 
 273     /**
 274      * Reset()
 275      */
 276     public void reset() {
 277         fValue.reset();
 278         fDeclaration = null;
 279         fTypeDecl = null;
 280         fSpecified = false;
 281         fValidationAttempted = AttributePSVI.VALIDATION_NONE;
 282         fValidity = AttributePSVI.VALIDITY_NOTKNOWN;
 283         fErrors = null;
 284         fValidationContext = null;
 285     }
 286 }