KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > IdentityConstraint


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * class for Identity Constraint.
30  *
31  * <p>This is the implementation of the Identity Constraint.
32  * The following properties are defined:</p>
33  * <p> 1. name : a NCName, defined in SchemaComponent.</p>
34  * <p> 2. target namespace : null or a namespace name, defined in SchemaComponent.</p>
35  * <p> 3. category : one of {CATEGORY_KEY, CATEGORY_KEYREF, CATEGORY_UNIQUE}.</p>
36  * <p> 4. selector : a restricted XPath expression. </p>
37  * <p> 5. fields : a non-empty list of restricted XPath expressions. </p>
38  * <p> 6. referenced key : required if category is KEYREF, otherwise forbidden.
39  * the referenced key, an identity constraint definition with category equal to
40  * key or unique and the name is equal to referenced name</p>
41  *
42  * <p>See XML Schema Part 1: Structures 3.11
43  * W3C Recommendation 2 May 2001</p>
44  *
45  * @see org.xquark.xml.schema.SchemaComponent
46  * @see org.xquark.xml.schema.ElementDeclaration
47  */

48 public class IdentityConstraint extends SchemaComponent {
49 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
50 private static final String JavaDoc RCSName = "$Name: $";
51
52   // constant
53
public static final int CATEGORY_KEY = 0;
54   public static final int CATEGORY_KEYREF = 1;
55   public static final int CATEGORY_UNIQUE = 2;
56   
57   // fields resovled in Loader
58
private int category;
59   private List JavaDoc selector = null;
60   private ArrayList JavaDoc fields = null;
61   private IdentityConstraint referencedKey = null; // initialization : keyref is resovled by keyrefName
62
private ElementDeclaration contextDecl;
63   
64   /**
65    * Create a new IdentityConstraint.
66    *
67    * @param schema The schema which contains this identity constraint.
68    * @param name The name of identity constraint, a NCName.
69    * @param category The category, one of {CATEGORY_KEY, CATEGORY_KEYREF, CATEGORY_UNIQUE}.
70    * @param contextDecl The context element declaration of identity constraint.
71    */

72   public IdentityConstraint(Schema schema, String JavaDoc name, int category, ElementDeclaration contextDecl) {
73     super(schema, name);
74     this.category = category;
75     this.contextDecl = contextDecl;
76   }
77   
78   public IdentityConstraint(Schema schema, String JavaDoc name, ElementDeclaration contextDecl) {
79     this(schema, name, CATEGORY_UNIQUE, contextDecl);
80   }
81   
82   public void accept(SchemaVisitor visitor) throws SchemaException {
83     visitor.visit(this);
84   }
85   
86   /**
87    * Look up the category
88    *
89    * @return The category, one of {CATEGORY_KEY, CATEGORY_KEYREF, CATEGORY_UNIQUE}.
90    */

91   public int getCategory() {
92     return this.category;
93   }
94   public void setCategory(int category) {
95     this.category = category;
96   }
97
98   /**
99    * Look up the context element declaration
100    *
101    * @return the context element declaration
102    */

103   public ElementDeclaration getContextDeclaration() {
104     return this.contextDecl;
105   }
106  
107  /**
108    * Look up the selector
109    *
110    * @return the list of XPath expressions defining the selector
111    */

112   public List JavaDoc getSelector() {
113     return this.selector;
114   }
115   public void setSelector(List JavaDoc selector) {
116     this.selector = selector;
117   }
118
119   /**
120    * Look up the identity constraint of referenced key
121    *
122    * @return The name of identity constraint of referenced key, if category is keyref; null, otherwise
123    */

124   public IdentityConstraint getReferencedKey() {
125     return referencedKey;
126   }
127   public void setReferencedKey(IdentityConstraint refer) {
128     this.referencedKey = refer;
129   }
130   
131   /**
132    * Look up the fields
133    *
134    * @return A list of lists of XPath expressions, each sublist defining a particular field
135    *
136    */

137   public ArrayList JavaDoc getFields() {
138     return this.fields;
139   }
140   public void addField(List JavaDoc field) {
141     if (fields == null)
142       fields = new ArrayList JavaDoc();
143     fields.add(field);
144   }
145   public void deleteFields() {
146     fields.clear();
147   }
148   
149   public boolean isEqualToKeyRefFields() {
150     if ( this.category == CATEGORY_KEYREF ) {
151       if ( this.fields != null && referencedKey != null && referencedKey.fields != null
152            && this.fields.size() == referencedKey.fields.size() )
153         return true;
154       else
155         return false;
156     }
157     else
158       return true;
159   }
160       
161 }
162
Popular Tags