1/*
2 * Copyright (c) 1997, 2017, 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
26package com.sun.xml.internal.xsom.impl;
27
28import com.sun.xml.internal.xsom.XSElementDecl;
29import com.sun.xml.internal.xsom.XSIdentityConstraint;
30import com.sun.xml.internal.xsom.XSXPath;
31import com.sun.xml.internal.xsom.impl.parser.SchemaDocumentImpl;
32import com.sun.xml.internal.xsom.visitor.XSFunction;
33import com.sun.xml.internal.xsom.visitor.XSVisitor;
34import org.xml.sax.Locator;
35
36import java.util.Collections;
37import java.util.List;
38
39/**
40 * {@link XSIdentityConstraint} implementation.
41 *
42 * @author Kohsuke Kawaguchi
43 */
44public 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 = (List<XSXPath>) Collections.unmodifiableList((List<? extends XSXPath>)fields);
63        for( XPathImpl xp : fields )
64            xp.setParent(this);
65        this.refer = refer;
66    }
67
68
69    @Override
70    public void visit(XSVisitor visitor) {
71        visitor.identityConstraint(this);
72    }
73
74    @Override
75    public <T> T apply(XSFunction<T> function) {
76        return function.identityConstraint(this);
77    }
78
79    public void setParent(ElementDecl parent) {
80        this.parent = parent;
81        parent.getOwnerSchema().addIdentityConstraint(this);
82    }
83
84    @Override
85    public XSElementDecl getParent() {
86        return parent;
87    }
88
89    @Override
90    public String getName() {
91        return name;
92    }
93
94    @Override
95    public String getTargetNamespace() {
96        return getParent().getTargetNamespace();
97    }
98
99    @Override
100    public short getCategory() {
101        return category;
102    }
103
104    @Override
105    public XSXPath getSelector() {
106        return selector;
107    }
108
109    @Override
110    public List<XSXPath> getFields() {
111        return fields;
112    }
113
114    @Override
115    public XSIdentityConstraint getReferencedKey() {
116        if(category==KEYREF)
117            return refer.get();
118        else
119            throw new IllegalStateException("not a keyref");
120    }
121
122    @Override
123    public XSIdentityConstraint get() {
124        return this;
125    }
126}
127