001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.xs.identity;
019:
020: import org.apache.xerces.impl.xpath.XPathException;
021: import org.apache.xerces.util.SymbolTable;
022: import org.apache.xerces.util.XMLChar;
023: import org.apache.xerces.xni.NamespaceContext;
024: import org.apache.xerces.xni.QName;
025: import org.apache.xerces.xni.XMLAttributes;
026: import org.apache.xerces.xs.ShortList;
027: import org.apache.xerces.xs.XSTypeDefinition;
028:
029: /**
030: * Schema identity constraint selector.
031: *
032: * @xerces.internal
033: *
034: * @author Andy Clark, IBM
035: * @version $Id: Selector.java 572110 2007-09-02 19:04:44Z mrglavas $
036: */
037: public class Selector {
038:
039: //
040: // Data
041: //
042:
043: /** XPath. */
044: protected final Selector.XPath fXPath;
045:
046: /** Identity constraint. */
047: protected final IdentityConstraint fIdentityConstraint;
048:
049: // the Identity constraint we're the matcher for. Only
050: // used for selectors!
051: protected IdentityConstraint fIDConstraint;
052:
053: //
054: // Constructors
055: //
056:
057: /** Constructs a selector. */
058: public Selector(Selector.XPath xpath,
059: IdentityConstraint identityConstraint) {
060: fXPath = xpath;
061: fIdentityConstraint = identityConstraint;
062: } // <init>(Selector.XPath,IdentityConstraint)
063:
064: //
065: // Public methods
066: //
067:
068: /** Returns the selector XPath. */
069: public org.apache.xerces.impl.xpath.XPath getXPath() {
070: return fXPath;
071: } // getXPath():org.apache.xerces.v1.schema.identity.XPath
072:
073: /** Returns the identity constraint. */
074: public IdentityConstraint getIDConstraint() {
075: return fIdentityConstraint;
076: } // getIDConstraint():IdentityConstraint
077:
078: // factory method
079:
080: /** Creates a selector matcher.
081: * @param activator The activator for this selector's fields.
082: * @param initialDepth The depth in the document at which this matcher began its life;
083: * used in correctly handling recursive elements.
084: */
085: public XPathMatcher createMatcher(FieldActivator activator,
086: int initialDepth) {
087: return new Selector.Matcher(fXPath, activator, initialDepth);
088: } // createMatcher(FieldActivator):XPathMatcher
089:
090: //
091: // Object methods
092: //
093:
094: /** Returns a string representation of this object. */
095: public String toString() {
096: return fXPath.toString();
097: } // toString():String
098:
099: //
100: // Classes
101: //
102:
103: /**
104: * Schema identity constraint selector XPath expression.
105: *
106: * @author Andy Clark, IBM
107: * @version $Id: Selector.java 572110 2007-09-02 19:04:44Z mrglavas $
108: */
109: public static class XPath extends
110: org.apache.xerces.impl.xpath.XPath {
111:
112: //
113: // Constructors
114: //
115:
116: /** Constructs a selector XPath expression. */
117: public XPath(String xpath, SymbolTable symbolTable,
118: NamespaceContext context) throws XPathException {
119: super (normalize(xpath), symbolTable, context);
120: // verify that an attribute is not selected
121: for (int i = 0; i < fLocationPaths.length; i++) {
122: org.apache.xerces.impl.xpath.XPath.Axis axis = fLocationPaths[i].steps[fLocationPaths[i].steps.length - 1].axis;
123: if (axis.type == XPath.Axis.ATTRIBUTE) {
124: throw new XPathException("c-selector-xpath");
125: }
126: }
127:
128: } // <init>(String,SymbolTable,NamespacesScope)
129:
130: private static String normalize(String xpath) {
131: // NOTE: We have to prefix the selector XPath with "./" in
132: // order to handle selectors such as "." that select
133: // the element container because the fields could be
134: // relative to that element. -Ac
135: // Unless xpath starts with a descendant node -Achille Fokoue
136: // ... or a '.' or a '/' - NG
137: // And we also need to prefix exprs to the right of | with ./ - NG
138: StringBuffer modifiedXPath = new StringBuffer(xpath
139: .length() + 5);
140: int unionIndex = -1;
141: do {
142: if (!(XMLChar.trim(xpath).startsWith("/") || XMLChar
143: .trim(xpath).startsWith("."))) {
144: modifiedXPath.append("./");
145: }
146: unionIndex = xpath.indexOf('|');
147: if (unionIndex == -1) {
148: modifiedXPath.append(xpath);
149: break;
150: }
151: modifiedXPath
152: .append(xpath.substring(0, unionIndex + 1));
153: xpath = xpath.substring(unionIndex + 1, xpath.length());
154: } while (true);
155: return modifiedXPath.toString();
156: }
157:
158: } // class Selector.XPath
159:
160: /**
161: * Selector matcher.
162: *
163: * @author Andy Clark, IBM
164: */
165: public class Matcher extends XPathMatcher {
166:
167: //
168: // Data
169: //
170:
171: /** Field activator. */
172: protected final FieldActivator fFieldActivator;
173:
174: /** Initial depth in the document at which this matcher was created. */
175: protected final int fInitialDepth;
176:
177: /** Element depth. */
178: protected int fElementDepth;
179:
180: /** Depth at match. */
181: protected int fMatchedDepth;
182:
183: //
184: // Constructors
185: //
186:
187: /** Constructs a selector matcher. */
188: public Matcher(Selector.XPath xpath, FieldActivator activator,
189: int initialDepth) {
190: super (xpath);
191: fFieldActivator = activator;
192: fInitialDepth = initialDepth;
193: } // <init>(Selector.XPath,FieldActivator)
194:
195: //
196: // XMLDocumentFragmentHandler methods
197: //
198:
199: public void startDocumentFragment() {
200: super .startDocumentFragment();
201: fElementDepth = 0;
202: fMatchedDepth = -1;
203: } // startDocumentFragment()
204:
205: /**
206: * The start of an element. If the document specifies the start element
207: * by using an empty tag, then the startElement method will immediately
208: * be followed by the endElement method, with no intervening methods.
209: *
210: * @param element The name of the element.
211: * @param attributes The element attributes.
212: *
213: */
214: public void startElement(QName element, XMLAttributes attributes) {
215: super .startElement(element, attributes);
216: fElementDepth++;
217: // activate the fields, if selector is matched
218: //int matched = isMatched();
219:
220: if (isMatched()) {
221: /* (fMatchedDepth == -1 && ((matched & MATCHED) == MATCHED)) ||
222: ((matched & MATCHED_DESCENDANT) == MATCHED_DESCENDANT)) { */
223: fMatchedDepth = fElementDepth;
224: fFieldActivator.startValueScopeFor(fIdentityConstraint,
225: fInitialDepth);
226: int count = fIdentityConstraint.getFieldCount();
227: for (int i = 0; i < count; i++) {
228: Field field = fIdentityConstraint.getFieldAt(i);
229: XPathMatcher matcher = fFieldActivator
230: .activateField(field, fInitialDepth);
231: matcher.startElement(element, attributes);
232: }
233: }
234:
235: } // startElement(QName,XMLAttrList,int)
236:
237: public void endElement(QName element, XSTypeDefinition type,
238: boolean nillable, Object actualValue, short valueType,
239: ShortList itemValueType) {
240: super .endElement(element, type, nillable, actualValue,
241: valueType, itemValueType);
242: if (fElementDepth-- == fMatchedDepth) {
243: fMatchedDepth = -1;
244: fFieldActivator.endValueScopeFor(fIdentityConstraint,
245: fInitialDepth);
246: }
247: }
248:
249: /** Returns the identity constraint. */
250: public IdentityConstraint getIdentityConstraint() {
251: return fIdentityConstraint;
252: } // getIdentityConstraint():IdentityConstraint
253:
254: /** get the initial depth at which this selector matched. */
255: public int getInitialDepth() {
256: return fInitialDepth;
257: } // getInitialDepth(): int
258:
259: } // class Matcher
260:
261: } // class Selector
|