1 22 23 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 { 37 private static final String RCSRevision = "$Revision: 1.1 $"; 38 private static final String RCSName = "$Name: $"; 39 40 static final String LOCAL = "localEntry"; 41 static final String PROPAGATED = "propagatedEntry"; 42 static final String INVALID = "invalidEntry"; 43 44 IdentityConstraint identityConstraint = null; 45 HashMap nodeTable = null; 46 List pendingRefs = null; 47 IdentityConstraintBinding reference = null; 48 49 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 prev = null; 70 switch (identityConstraint.getCategory()) { 71 case IdentityConstraint.CATEGORY_UNIQUE: 72 if (nodeTable == null) nodeTable = new HashMap(); 73 prev = (String )nodeTable.put(keySequence, LOCAL); 74 if (prev == LOCAL) 75 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 )nodeTable.put(keySequence, LOCAL); 84 if (prev == LOCAL) 85 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 key = it.next(); 105 String prev = (String )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 buffer = null; 117 if (table == null) { 118 buffer = new StringBuffer (); 119 while (it.hasNext()) { 120 buffer.append(" "); 121 buffer.append(it.next()); 122 } 123 } else { 124 buffer = new StringBuffer (); 125 while (it.hasNext()) { 126 List sequence = (List)it.next(); 127 String result = (String )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 |