1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   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  * Copyright (C) 2004-2011
  27  *
  28  * Permission is hereby granted, free of charge, to any person obtaining a copy
  29  * of this software and associated documentation files (the "Software"), to deal
  30  * in the Software without restriction, including without limitation the rights
  31  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  32  * copies of the Software, and to permit persons to whom the Software is
  33  * furnished to do so, subject to the following conditions:
  34  *
  35  * The above copyright notice and this permission notice shall be included in
  36  * all copies or substantial portions of the Software.
  37  *
  38  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  39  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  40  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  42  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  43  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  44  * THE SOFTWARE.
  45  */
  46 package com.sun.xml.internal.rngom.binary;
  47 
  48 import com.sun.xml.internal.rngom.binary.visitor.PatternFunction;
  49 import com.sun.xml.internal.rngom.binary.visitor.PatternVisitor;
  50 import com.sun.xml.internal.rngom.nc.NameClass;
  51 import com.sun.xml.internal.rngom.nc.SimpleNameClass;
  52 import org.xml.sax.Locator;
  53 import org.xml.sax.SAXException;
  54 
  55 public final class AttributePattern extends Pattern {
  56   private NameClass nameClass;
  57   private Pattern p;
  58   private Locator loc;
  59 
  60   AttributePattern(NameClass nameClass, Pattern value, Locator loc) {
  61     super(false,
  62           EMPTY_CONTENT_TYPE,
  63           combineHashCode(ATTRIBUTE_HASH_CODE,
  64                           nameClass.hashCode(),
  65                           value.hashCode()));
  66     this.nameClass = nameClass;
  67     this.p = value;
  68     this.loc = loc;
  69   }
  70 
  71   Pattern expand(SchemaPatternBuilder b) {
  72     Pattern ep = p.expand(b);
  73     if (ep != p)
  74       return b.makeAttribute(nameClass, ep, loc);
  75     else
  76       return this;
  77   }
  78 
  79   void checkRestrictions(int context, DuplicateAttributeDetector dad, Alphabet alpha)
  80     throws RestrictionViolationException {
  81     switch (context) {
  82     case START_CONTEXT:
  83       throw new RestrictionViolationException("start_contains_attribute");
  84     case ELEMENT_CONTEXT:
  85       if (nameClass.isOpen())
  86         throw new RestrictionViolationException("open_name_class_not_repeated");
  87       break;
  88     case ELEMENT_REPEAT_GROUP_CONTEXT:
  89       throw new RestrictionViolationException("one_or_more_contains_group_contains_attribute");
  90     case ELEMENT_REPEAT_INTERLEAVE_CONTEXT:
  91       throw new RestrictionViolationException("one_or_more_contains_interleave_contains_attribute");
  92     case LIST_CONTEXT:
  93       throw new RestrictionViolationException("list_contains_attribute");
  94     case ATTRIBUTE_CONTEXT:
  95       throw new RestrictionViolationException("attribute_contains_attribute");
  96     case DATA_EXCEPT_CONTEXT:
  97       throw new RestrictionViolationException("data_except_contains_attribute");
  98     }
  99     if (!dad.addAttribute(nameClass)) {
 100       if (nameClass instanceof SimpleNameClass)
 101         throw new RestrictionViolationException("duplicate_attribute_detail", ((SimpleNameClass)nameClass).name);
 102       else
 103         throw new RestrictionViolationException("duplicate_attribute");
 104     }
 105     try {
 106       p.checkRestrictions(ATTRIBUTE_CONTEXT, null, null);
 107     }
 108     catch (RestrictionViolationException e) {
 109       e.maybeSetLocator(loc);
 110       throw e;
 111     }
 112   }
 113 
 114   boolean samePattern(Pattern other) {
 115     if (!(other instanceof AttributePattern))
 116       return false;
 117     AttributePattern ap = (AttributePattern)other;
 118     return nameClass.equals(ap.nameClass)&& p == ap.p;
 119   }
 120 
 121   void checkRecursion(int depth) throws SAXException {
 122     p.checkRecursion(depth);
 123   }
 124 
 125   public void accept(PatternVisitor visitor) {
 126     visitor.visitAttribute(nameClass, p);
 127   }
 128 
 129   public Object apply(PatternFunction f) {
 130     return f.caseAttribute(this);
 131   }
 132 
 133   public Pattern getContent() {
 134     return p;
 135   }
 136 
 137   public NameClass getNameClass() {
 138     return nameClass;
 139   }
 140 
 141   public Locator getLocator() {
 142     return loc;
 143   }
 144 }