aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.xsom; aoqi@0: aoqi@0: import com.sun.xml.internal.xsom.impl.scd.Iterators; aoqi@0: import com.sun.xml.internal.xsom.impl.scd.ParseException; aoqi@0: import com.sun.xml.internal.xsom.impl.scd.SCDImpl; aoqi@0: import com.sun.xml.internal.xsom.impl.scd.SCDParser; aoqi@0: import com.sun.xml.internal.xsom.impl.scd.Step; aoqi@0: import com.sun.xml.internal.xsom.impl.scd.TokenMgrError; aoqi@0: import com.sun.xml.internal.xsom.util.DeferedCollection; aoqi@0: aoqi@0: import javax.xml.namespace.NamespaceContext; aoqi@0: import java.util.Collection; aoqi@0: import java.util.Iterator; aoqi@0: import java.util.List; aoqi@0: aoqi@0: /** aoqi@0: * Schema Component Designator (SCD). aoqi@0: * aoqi@0: *

aoqi@0: * SCD for schema is what XPath is for XML. SCD allows you to select a schema component(s) aoqi@0: * from a schema component(s). aoqi@0: * aoqi@0: *

aoqi@0: * See XML Schema: Component Designators. aoqi@0: * This implementation is based on 03/29/2005 working draft. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public abstract class SCD { aoqi@0: aoqi@0: /** aoqi@0: * Parses the string representation of SCD. aoqi@0: * aoqi@0: *

aoqi@0: * This method involves parsing the path expression and preparing the in-memory aoqi@0: * structure, so this is useful when you plan to use the same SCD against aoqi@0: * different context node multiple times. aoqi@0: * aoqi@0: *

aoqi@0: * If you want to evaluate SCD just once, use {@link XSComponent#select} methods. aoqi@0: * aoqi@0: * @param path aoqi@0: * the string representation of SCD, such as "/foo/bar". aoqi@0: * @param nsContext aoqi@0: * Its {@link NamespaceContext#getNamespaceURI(String)} is used aoqi@0: * to resolve prefixes in the SCD to the namespace URI. aoqi@0: */ aoqi@0: public static SCD create(String path, NamespaceContext nsContext) throws java.text.ParseException { aoqi@0: try { aoqi@0: SCDParser p = new SCDParser(path,nsContext); aoqi@0: List list = p.RelativeSchemaComponentPath(); aoqi@0: return new SCDImpl(path,list.toArray(new Step[list.size()])); aoqi@0: } catch (TokenMgrError e) { aoqi@0: throw setCause(new java.text.ParseException(e.getMessage(), -1 ),e); aoqi@0: } catch (ParseException e) { aoqi@0: throw setCause(new java.text.ParseException(e.getMessage(), e.currentToken.beginColumn ),e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static java.text.ParseException setCause(java.text.ParseException e, Throwable x) { aoqi@0: e.initCause(x); aoqi@0: return e; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Evaluates the SCD against the given context node and aoqi@0: * returns the matched nodes. aoqi@0: * aoqi@0: * @return aoqi@0: * could be empty but never be null. aoqi@0: */ aoqi@0: public final Collection select(XSComponent contextNode) { aoqi@0: return new DeferedCollection(select(Iterators.singleton(contextNode))); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Evaluates the SCD against the whole schema and aoqi@0: * returns the matched nodes. aoqi@0: * aoqi@0: *

aoqi@0: * This method is here because {@link XSSchemaSet} aoqi@0: * doesn't implement {@link XSComponent}. aoqi@0: * aoqi@0: * @return aoqi@0: * could be empty but never be null. aoqi@0: */ aoqi@0: public final Collection select(XSSchemaSet contextNode) { aoqi@0: return select(contextNode.getSchemas()); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Evaluates the SCD against the given context node and aoqi@0: * returns the matched node. aoqi@0: * aoqi@0: * @return aoqi@0: * null if the SCD didn't match anything. If the SCD matched more than one node, aoqi@0: * the first one will be returned. aoqi@0: */ aoqi@0: public final XSComponent selectSingle(XSComponent contextNode) { aoqi@0: Iterator r = select(Iterators.singleton(contextNode)); aoqi@0: if(r.hasNext()) return r.next(); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Evaluates the SCD against the whole schema set and aoqi@0: * returns the matched node. aoqi@0: * aoqi@0: * @return aoqi@0: * null if the SCD didn't match anything. If the SCD matched more than one node, aoqi@0: * the first one will be returned. aoqi@0: */ aoqi@0: public final XSComponent selectSingle(XSSchemaSet contextNode) { aoqi@0: Iterator r = select(contextNode.iterateSchema()); aoqi@0: if(r.hasNext()) return r.next(); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Evaluates the SCD against the given set of context nodes and aoqi@0: * returns the matched nodes. aoqi@0: * aoqi@0: * @param contextNodes aoqi@0: * {@link XSComponent}s that represent the context node against aoqi@0: * which {@link SCD} is evaluated. aoqi@0: * aoqi@0: * @return aoqi@0: * could be empty but never be null. aoqi@0: */ aoqi@0: public abstract Iterator select(Iterator contextNodes); aoqi@0: aoqi@0: /** aoqi@0: * Evaluates the SCD against the given set of context nodes and aoqi@0: * returns the matched nodes. aoqi@0: * aoqi@0: * @param contextNodes aoqi@0: * {@link XSComponent}s that represent the context node against aoqi@0: * which {@link SCD} is evaluated. aoqi@0: * aoqi@0: * @return aoqi@0: * could be empty but never be null. aoqi@0: */ aoqi@0: public final Collection select(Collection contextNodes) { aoqi@0: return new DeferedCollection(select(contextNodes.iterator())); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the textual SCD representation as given to {@link SCD#create(String, NamespaceContext)}. aoqi@0: */ aoqi@0: public abstract String toString(); aoqi@0: }