1 /*
   2  * Copyright (c) 1997, 2012, 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 package com.sun.xml.internal.xsom.impl;
  27 
  28 import com.sun.xml.internal.xsom.XSElementDecl;
  29 import com.sun.xml.internal.xsom.XSIdentityConstraint;
  30 import com.sun.xml.internal.xsom.XSXPath;
  31 import com.sun.xml.internal.xsom.impl.parser.SchemaDocumentImpl;
  32 import com.sun.xml.internal.xsom.visitor.XSFunction;
  33 import com.sun.xml.internal.xsom.visitor.XSVisitor;
  34 import org.xml.sax.Locator;
  35 
  36 import java.util.Collections;
  37 import java.util.List;
  38 
  39 /**
  40  * {@link XSIdentityConstraint} implementation.
  41  *
  42  * @author Kohsuke Kawaguchi
  43  */
  44 public class IdentityConstraintImpl extends ComponentImpl implements XSIdentityConstraint, Ref.IdentityConstraint {
  45 
  46     private XSElementDecl parent;
  47     private final short category;
  48     private final String name;
  49     private final XSXPath selector;
  50     private final List<XSXPath> fields;
  51     private final Ref.IdentityConstraint refer;
  52 
  53     public IdentityConstraintImpl(SchemaDocumentImpl _owner, AnnotationImpl _annon, Locator _loc,
  54         ForeignAttributesImpl fa, short category, String name, XPathImpl selector,
  55         List<XPathImpl> fields, Ref.IdentityConstraint refer) {
  56 
  57         super(_owner, _annon, _loc, fa);
  58         this.category = category;
  59         this.name = name;
  60         this.selector = selector;
  61         selector.setParent(this);
  62         this.fields = Collections.unmodifiableList((List<? extends XSXPath>)fields);
  63         for( XPathImpl xp : fields )
  64             xp.setParent(this);
  65         this.refer = refer;
  66     }
  67 
  68 
  69     public void visit(XSVisitor visitor) {
  70         visitor.identityConstraint(this);
  71     }
  72 
  73     public <T> T apply(XSFunction<T> function) {
  74         return function.identityConstraint(this);
  75     }
  76 
  77     public void setParent(ElementDecl parent) {
  78         this.parent = parent;
  79         parent.getOwnerSchema().addIdentityConstraint(this);
  80     }
  81 
  82     public XSElementDecl getParent() {
  83         return parent;
  84     }
  85 
  86     public String getName() {
  87         return name;
  88     }
  89 
  90     public String getTargetNamespace() {
  91         return getParent().getTargetNamespace();
  92     }
  93 
  94     public short getCategory() {
  95         return category;
  96     }
  97 
  98     public XSXPath getSelector() {
  99         return selector;
 100     }
 101 
 102     public List<XSXPath> getFields() {
 103         return fields;
 104     }
 105 
 106     public XSIdentityConstraint getReferencedKey() {
 107         if(category==KEYREF)
 108             return refer.get();
 109         else
 110             throw new IllegalStateException("not a keyref");
 111     }
 112 
 113     public XSIdentityConstraint get() {
 114         return this;
 115     }
 116 }