| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.event; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.Location; |
| 11 |
import net.sf.saxon.om.NodeName; |
| 12 |
import net.sf.saxon.om.StandardNames; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.type.AtomicType; |
| 15 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 16 |
import net.sf.saxon.type.SchemaType; |
| 17 |
import net.sf.saxon.type.SimpleType; |
| 18 |
|
| 19 |
import java.util.HashSet; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class IDFilter extends StartTagBuffer { |
| 29 |
|
| 30 |
private String requiredId; |
| 31 |
private int activeDepth = 0; |
| 32 |
private boolean matched = false; |
| 33 |
private HashSet<SimpleType> nonIDs; |
| 34 |
|
| 35 |
public IDFilter(Receiver next, String id) { |
| 36 |
|
| 37 |
super(next); |
| 38 |
this.requiredId = id; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 46 |
matched = false; |
| 47 |
if (activeDepth > 0) { |
| 48 |
activeDepth++; |
| 49 |
} |
| 50 |
super.startElement(nameCode, typeCode, location, properties); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException { |
| 71 |
super.attribute(attName, typeCode, value, locationId, properties); |
| 72 |
if ((attName.equals(StandardNames.XML_ID_NAME)) || ((properties & ReceiverOptions.IS_ID) != 0)) { |
| 73 |
if (value.toString().equals(requiredId)) { |
| 74 |
matched = true; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public void startContent() throws XPathException { |
| 84 |
if (activeDepth > 0) { |
| 85 |
super.startContent(); |
| 86 |
} else if (matched) { |
| 87 |
activeDepth = 1; |
| 88 |
super.startContent(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
protected void declareNamespacesForStartElement() throws XPathException { |
| 93 |
if (activeDepth == 1) { |
| 94 |
declareAllNamespaces(); |
| 95 |
} else { |
| 96 |
super.declareNamespacesForStartElement(); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public void endElement() throws XPathException { |
| 105 |
if (activeDepth > 0) { |
| 106 |
nextReceiver.endElement(); |
| 107 |
activeDepth--; |
| 108 |
} else { |
| 109 |
undeclareNamespacesForElement(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 118 |
if (activeDepth > 0) { |
| 119 |
super.characters(chars, locationId, properties); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException { |
| 128 |
if (activeDepth > 0) { |
| 129 |
super.processingInstruction(target, data, locationId, properties); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public void comment(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 138 |
if (activeDepth > 0) { |
| 139 |
super.comment(chars, locationId, properties); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public boolean usesTypeAnnotations() { |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private boolean isIDCode(SimpleType typeCode) { |
| 164 |
if (typeCode == BuiltInAtomicType.ID) { |
| 165 |
return true; |
| 166 |
} |
| 167 |
if (typeCode instanceof BuiltInAtomicType) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
if (nonIDs == null) { |
| 172 |
nonIDs = new HashSet<SimpleType>(20); |
| 173 |
} |
| 174 |
if (nonIDs.contains(typeCode)) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
if (typeCode.isAtomicType()) { |
| 178 |
if (getConfiguration().getTypeHierarchy().isSubType((AtomicType) typeCode, BuiltInAtomicType.ID)) { |
| 179 |
return true; |
| 180 |
} else { |
| 181 |
nonIDs.add(typeCode); |
| 182 |
return false; |
| 183 |
} |
| 184 |
} else { |
| 185 |
return false; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
|