1 package net.sf.saxon.event; 2 import net.sf.saxon.trans.XPathException; 3 import net.sf.saxon.style.StandardNames; 4 import net.sf.saxon.om.NamePool; 5 import net.sf.saxon.type.SchemaType; 6 import net.sf.saxon.type.AtomicType; 7 import net.sf.saxon.type.Type; 8 9 import java.util.HashSet ; 10 11 12 17 18 public class IDFilter extends StartTagBuffer { 19 20 private String requiredId; 21 private int activeDepth = 0; 22 private boolean matched = false; 23 private HashSet nonIDs; 24 25 public IDFilter (String id) { 26 this.requiredId = id; 28 } 29 30 33 34 public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException { 35 matched = false; 36 if (activeDepth>0) { 37 activeDepth++; 38 } 39 super.startElement(nameCode, typeCode, locationId, properties); } 41 42 54 55 public void attribute(int nameCode, int typeCode, CharSequence value, int locationId, int properties) throws XPathException { 56 super.attribute(nameCode, typeCode, value, locationId, properties); 57 if ((nameCode & NamePool.FP_MASK) == StandardNames.XML_ID || isIDCode(typeCode)) { 58 if (value.toString().equals(requiredId)) { 59 matched = true; 60 } 61 } 62 } 63 64 67 68 public void startContent() throws XPathException { 69 if (activeDepth>0) { 70 super.startContent(); 71 } else if (matched) { 72 activeDepth = 1; 73 super.startContent(); 74 } 75 } 76 77 protected void declareNamespacesForStartElement() throws XPathException { 78 if (activeDepth == 1) { 79 declareAllNamespaces(); 80 } else { 81 super.declareNamespacesForStartElement(); 82 } 83 } 84 85 88 89 public void endElement() throws XPathException { 90 if (activeDepth > 0) { 91 super.endElement(); 92 activeDepth--; 93 } else { 94 undeclareNamespacesForElement(); 95 } 96 } 97 98 101 102 public void characters(CharSequence chars, int locationId, int properties) throws XPathException { 103 if (activeDepth > 0) { 104 super.characters(chars, locationId, properties); 105 } 106 } 107 108 111 112 public void processingInstruction(String target, CharSequence data, int locationId, int properties) throws XPathException { 113 if (activeDepth > 0) { 114 super.processingInstruction(target, data, locationId, properties); 115 } 116 } 117 118 121 122 public void comment(CharSequence chars, int locationId, int properties) throws XPathException { 123 if (activeDepth > 0) { 124 super.comment(chars, locationId, properties); 125 } 126 } 127 128 131 132 private boolean isIDCode(int typeCode) { 133 if ((typeCode & NamePool.FP_MASK) == StandardNames.XS_ID) { 134 return true; 135 } else if (typeCode < 1024) { 136 return false; 138 } else { 139 if (nonIDs == null) { 140 nonIDs = new HashSet (20); 141 } 142 Integer key = new Integer (typeCode); 143 if (nonIDs.contains(key)) { 144 return false; 145 } 146 SchemaType type = getConfiguration().getSchemaType(typeCode); 147 if (type instanceof AtomicType) { 148 if (Type.isSubType((AtomicType)type, Type.ID_TYPE)) { 149 return true; 150 } else { 151 nonIDs.add(key); 152 return false; 153 } 154 } else { 155 return false; 156 } 157 } 158 } 159 160 } 161 | Popular Tags |