286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001, 2002,2004,2005 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.impl.xs.identity;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.impl.xpath.XPathException;
286N/Aimport com.sun.org.apache.xerces.internal.util.SymbolTable;
286N/Aimport com.sun.org.apache.xerces.internal.util.XMLChar;
286N/Aimport com.sun.org.apache.xerces.internal.xni.NamespaceContext;
286N/Aimport com.sun.org.apache.xerces.internal.xni.QName;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLAttributes;
286N/Aimport com.sun.org.apache.xerces.internal.xs.ShortList;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
286N/A
286N/A/**
286N/A * Schema identity constraint selector.
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Andy Clark, IBM
286N/A * @version $Id: Selector.java,v 1.7 2010-11-01 04:39:57 joehw Exp $
286N/A */
286N/Apublic class Selector {
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A /** XPath. */
286N/A protected final Selector.XPath fXPath;
286N/A
286N/A /** Identity constraint. */
286N/A protected final IdentityConstraint fIdentityConstraint;
286N/A
286N/A // the Identity constraint we're the matcher for. Only
286N/A // used for selectors!
286N/A protected IdentityConstraint fIDConstraint;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Constructs a selector. */
286N/A public Selector(Selector.XPath xpath,
286N/A IdentityConstraint identityConstraint) {
286N/A fXPath = xpath;
286N/A fIdentityConstraint = identityConstraint;
286N/A } // <init>(Selector.XPath,IdentityConstraint)
286N/A
286N/A //
286N/A // Public methods
286N/A //
286N/A
286N/A /** Returns the selector XPath. */
286N/A public com.sun.org.apache.xerces.internal.impl.xpath.XPath getXPath() {
286N/A return fXPath;
286N/A } // getXPath():com.sun.org.apache.xerces.internal.v1.schema.identity.XPath
286N/A
286N/A /** Returns the identity constraint. */
286N/A public IdentityConstraint getIDConstraint() {
286N/A return fIdentityConstraint;
286N/A } // getIDConstraint():IdentityConstraint
286N/A
286N/A // factory method
286N/A
286N/A /** Creates a selector matcher.
286N/A * @param activator The activator for this selector's fields.
286N/A * @param initialDepth The depth in the document at which this matcher began its life;
286N/A * used in correctly handling recursive elements.
286N/A */
286N/A public XPathMatcher createMatcher(FieldActivator activator, int initialDepth) {
286N/A return new Selector.Matcher(fXPath, activator, initialDepth);
286N/A } // createMatcher(FieldActivator):XPathMatcher
286N/A
286N/A //
286N/A // Object methods
286N/A //
286N/A
286N/A /** Returns a string representation of this object. */
286N/A public String toString() {
286N/A return fXPath.toString();
286N/A } // toString():String
286N/A
286N/A //
286N/A // Classes
286N/A //
286N/A
286N/A /**
286N/A * Schema identity constraint selector XPath expression.
286N/A *
286N/A * @author Andy Clark, IBM
286N/A * @version $Id: Selector.java,v 1.7 2010-11-01 04:39:57 joehw Exp $
286N/A */
286N/A public static class XPath
286N/A extends com.sun.org.apache.xerces.internal.impl.xpath.XPath {
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Constructs a selector XPath expression. */
286N/A public XPath(String xpath, SymbolTable symbolTable,
286N/A NamespaceContext context) throws XPathException {
286N/A super(normalize(xpath), symbolTable, context);
286N/A // verify that an attribute is not selected
286N/A for (int i=0;i<fLocationPaths.length;i++) {
286N/A com.sun.org.apache.xerces.internal.impl.xpath.XPath.Axis axis =
286N/A fLocationPaths[i].steps[fLocationPaths[i].steps.length-1].axis;
286N/A if (axis.type == XPath.Axis.ATTRIBUTE) {
286N/A throw new XPathException("c-selector-xpath");
286N/A }
286N/A }
286N/A
286N/A } // <init>(String,SymbolTable,NamespacesScope)
286N/A
286N/A private static String normalize(String xpath) {
286N/A // NOTE: We have to prefix the selector XPath with "./" in
286N/A // order to handle selectors such as "." that select
286N/A // the element container because the fields could be
286N/A // relative to that element. -Ac
286N/A // Unless xpath starts with a descendant node -Achille Fokoue
286N/A // ... or a '.' or a '/' - NG
286N/A // And we also need to prefix exprs to the right of | with ./ - NG
286N/A StringBuffer modifiedXPath = new StringBuffer(xpath.length()+5);
286N/A int unionIndex = -1;
286N/A do {
286N/A if(!(XMLChar.trim(xpath).startsWith("/") || XMLChar.trim(xpath).startsWith("."))) {
286N/A modifiedXPath.append("./");
286N/A }
286N/A unionIndex = xpath.indexOf('|');
286N/A if(unionIndex == -1) {
286N/A modifiedXPath.append(xpath);
286N/A break;
286N/A }
286N/A modifiedXPath.append(xpath.substring(0,unionIndex+1));
286N/A xpath = xpath.substring(unionIndex+1, xpath.length());
286N/A } while(true);
286N/A return modifiedXPath.toString();
286N/A }
286N/A
286N/A } // class Selector.XPath
286N/A
286N/A /**
286N/A * Selector matcher.
286N/A *
286N/A * @author Andy Clark, IBM
286N/A */
286N/A public class Matcher
286N/A extends XPathMatcher {
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A /** Field activator. */
286N/A protected final FieldActivator fFieldActivator;
286N/A
286N/A /** Initial depth in the document at which this matcher was created. */
286N/A protected final int fInitialDepth;
286N/A
286N/A /** Element depth. */
286N/A protected int fElementDepth;
286N/A
286N/A /** Depth at match. */
286N/A protected int fMatchedDepth;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Constructs a selector matcher. */
286N/A public Matcher(Selector.XPath xpath, FieldActivator activator,
286N/A int initialDepth) {
286N/A super(xpath);
286N/A fFieldActivator = activator;
286N/A fInitialDepth = initialDepth;
286N/A } // <init>(Selector.XPath,FieldActivator)
286N/A
286N/A //
286N/A // XMLDocumentFragmentHandler methods
286N/A //
286N/A
286N/A public void startDocumentFragment(){
286N/A super.startDocumentFragment();
286N/A fElementDepth = 0;
286N/A fMatchedDepth = -1;
286N/A } // startDocumentFragment()
286N/A
286N/A /**
286N/A * The start of an element. If the document specifies the start element
286N/A * by using an empty tag, then the startElement method will immediately
286N/A * be followed by the endElement method, with no intervening methods.
286N/A *
286N/A * @param element The name of the element.
286N/A * @param attributes The element attributes.
286N/A *
286N/A */
286N/A public void startElement(QName element, XMLAttributes attributes) {
286N/A super.startElement(element, attributes);
286N/A fElementDepth++;
286N/A // activate the fields, if selector is matched
286N/A //int matched = isMatched();
286N/A
286N/A if (isMatched()) {
286N/A/* (fMatchedDepth == -1 && ((matched & MATCHED) == MATCHED)) ||
286N/A ((matched & MATCHED_DESCENDANT) == MATCHED_DESCENDANT)) { */
286N/A fMatchedDepth = fElementDepth;
286N/A fFieldActivator.startValueScopeFor(fIdentityConstraint, fInitialDepth);
286N/A int count = fIdentityConstraint.getFieldCount();
286N/A for (int i = 0; i < count; i++) {
286N/A Field field = fIdentityConstraint.getFieldAt(i);
286N/A XPathMatcher matcher = fFieldActivator.activateField(field, fInitialDepth);
286N/A matcher.startElement(element, attributes);
286N/A }
286N/A }
286N/A
286N/A } // startElement(QName,XMLAttrList,int)
286N/A
286N/A public void endElement(QName element, XSTypeDefinition type, boolean nillable, Object actualValue, short valueType, ShortList itemValueType) {
286N/A super.endElement(element, type, nillable, actualValue, valueType, itemValueType);
286N/A if (fElementDepth-- == fMatchedDepth) {
286N/A fMatchedDepth = -1;
286N/A fFieldActivator.endValueScopeFor(fIdentityConstraint, fInitialDepth);
286N/A }
286N/A }
286N/A
286N/A /** Returns the identity constraint. */
286N/A public IdentityConstraint getIdentityConstraint() {
286N/A return fIdentityConstraint;
286N/A } // getIdentityConstraint():IdentityConstraint
286N/A
286N/A /** get the initial depth at which this selector matched. */
286N/A public int getInitialDepth() {
286N/A return fInitialDepth;
286N/A } // getInitialDepth(): int
286N/A
286N/A
286N/A } // class Matcher
286N/A
286N/A} // class Selector