KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > validation > IdentityConstraintBinding


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 /*
24  * IdentityConstraintPSVI.java
25  *
26  * Created on 1 juin 2001, 14:16
27  */

28
29 package org.xquark.schema.validation;
30
31 import java.util.*;
32
33 import org.xquark.schema.IdentityConstraint;
34 import org.xquark.schema.SchemaException;
35
36 public class IdentityConstraintBinding extends Object JavaDoc {
37 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
38 private static final String JavaDoc RCSName = "$Name: $";
39
40   static final String JavaDoc LOCAL = "localEntry";
41   static final String JavaDoc PROPAGATED = "propagatedEntry";
42   static final String JavaDoc INVALID = "invalidEntry";
43   
44   IdentityConstraint identityConstraint = null;
45   HashMap nodeTable = null;
46   List pendingRefs = null;
47   IdentityConstraintBinding reference = null;
48   
49   /** Creates new IdentityConstraintPSVI */
50   public IdentityConstraintBinding(IdentityConstraint identityConstraint) {
51     this.identityConstraint = identityConstraint;
52   }
53   
54   public IdentityConstraint getDefinition() {
55     return identityConstraint;
56   }
57   
58   public Map getNodeTable() { return nodeTable; }
59   
60   public IdentityConstraintBinding getReference() {
61     return reference;
62   }
63   
64   void setReference(IdentityConstraintBinding reference) {
65     this.reference = reference;
66   }
67   
68   void registerEntry(List keySequence) throws SchemaException {
69     String JavaDoc prev = null;
70     switch (identityConstraint.getCategory()) {
71     case IdentityConstraint.CATEGORY_UNIQUE:
72       if (nodeTable == null) nodeTable = new HashMap();
73       prev = (String JavaDoc)nodeTable.put(keySequence, LOCAL);
74       if (prev == LOCAL)
75       // to do: if type is binary or base64Binary, don't add keySequence
76
// else add keySequence
77
// noted by ZL, 08/07/2002
78
throw new SchemaException("cvc-identity-constraint.4.1", identityConstraint,
79                                    new SchemaException(null, "key: "+keySequence));
80       break;
81     case IdentityConstraint.CATEGORY_KEY:
82       if (nodeTable == null) nodeTable = new HashMap();
83       prev = (String JavaDoc)nodeTable.put(keySequence, LOCAL);
84       if (prev == LOCAL)
85       // to do: if type is binary or base64Binary, don't add keySequence
86
// else add keySequence
87
// noted by ZL, 08/07/2002
88
throw new SchemaException("cvc-identity-constraint.4.2.2", identityConstraint,
89                                    new SchemaException(null, "key: "+keySequence));
90       break;
91     case IdentityConstraint.CATEGORY_KEYREF:
92       if (pendingRefs == null) pendingRefs = new ArrayList();
93       pendingRefs.add(keySequence);
94       break;
95     }
96   }
97   
98   void propagateEntries(IdentityConstraintBinding binding) {
99     Map table = binding.getNodeTable();
100     if (table != null) {
101       if (nodeTable == null) nodeTable = new HashMap();
102       Iterator it = table.keySet().iterator();
103       while (it.hasNext()) {
104         Object JavaDoc key = it.next();
105         String JavaDoc prev = (String JavaDoc)nodeTable.get(key);
106         if (prev == null) nodeTable.put(key, PROPAGATED);
107         else if (prev == PROPAGATED) nodeTable.put(key, INVALID);
108       }
109     }
110   }
111   
112   void checkPendingRefs() throws SchemaException {
113     if (reference != null && pendingRefs != null ) {
114       Iterator it = pendingRefs.iterator();
115       Map table = reference.getNodeTable();
116       StringBuffer JavaDoc buffer = null;
117       if (table == null) {
118         buffer = new StringBuffer JavaDoc();
119         while (it.hasNext()) {
120           buffer.append(" ");
121           buffer.append(it.next());
122         }
123       } else {
124         buffer = new StringBuffer JavaDoc();
125         while (it.hasNext()) {
126           List sequence = (List)it.next();
127           String JavaDoc result = (String JavaDoc)table.get(sequence);
128           if (result == null || result == INVALID) {
129             buffer.append(" ");
130             buffer.append(sequence);
131           }
132         }
133       }
134       if (buffer.length() > 0)
135         throw new SchemaException("cvc-identity-constraint.4.3", identityConstraint,
136                                   new SchemaException(null, "key sequences:"+buffer));
137     }
138   }
139 }
140
Popular Tags