1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.rngom.nc;
  27 
  28 import com.sun.xml.internal.rngom.ast.om.ParsedNameClass;
  29 
  30 import javax.xml.namespace.QName;
  31 import java.io.Serializable;
  32 import java.util.HashSet;
  33 import java.util.Set;
  34 
  35 /**
  36  * Name class is a set of {@link QName}s.
  37  */
  38 public abstract class NameClass implements ParsedNameClass, Serializable {
  39     static final int SPECIFICITY_NONE = -1;
  40     static final int SPECIFICITY_ANY_NAME = 0;
  41     static final int SPECIFICITY_NS_NAME = 1;
  42     static final int SPECIFICITY_NAME = 2;
  43 
  44     /**
  45      * Returns true if the given {@link QName} is a valid name
  46      * for this QName.
  47      */
  48     public abstract boolean contains(QName name);
  49 
  50     public abstract int containsSpecificity(QName name);
  51 
  52     /**
  53      * Visitor pattern support.
  54      */
  55     public abstract <V> V accept(NameClassVisitor<V> visitor);
  56 
  57     /**
  58      * Returns true if the name class accepts infinite number of
  59      * {@link QName}s.
  60      *
  61      * <p>
  62      * Intuitively, this method returns true if the name class is
  63      * some sort of wildcard.
  64      */
  65     public abstract boolean isOpen();
  66 
  67     /**
  68      * If the name class is closed (IOW !{@link #isOpen()}),
  69      * return the set of names in this name class. Otherwise the behavior
  70      * is undefined.
  71      */
  72     public Set<QName> listNames() {
  73         final Set<QName> names = new HashSet<QName>();
  74         accept(new NameClassWalker() {
  75             public Void visitName(QName name) {
  76                 names.add(name);
  77                 return null;
  78             }
  79         });
  80         return names;
  81     }
  82 
  83     /**
  84      * Returns true if the intersection between this name class
  85      * and the specified name class is non-empty.
  86      */
  87     public final boolean hasOverlapWith( NameClass nc2 ) {
  88         return OverlapDetector.overlap(this,nc2);
  89     }
  90 
  91 
  92     /** Sigleton instance that represents "anyName". */
  93     public static final NameClass ANY = new AnyNameClass();
  94 
  95     /**
  96      * Sigleton instance that accepts no name.
  97      *
  98      * <p>
  99      * This instance is useful when doing boolean arithmetic over
 100      * name classes (such as computing an inverse of a given name class, etc),
 101      * even though it can never appear in a RELAX NG surface syntax.
 102      *
 103      * <p>
 104      * Internally, this instance is also used for:
 105      * <ol>
 106      *  <li>Used to recover from errors during parsing.
 107      *  <li>Mark element patterns with &lt;notAllowed/> content model.
 108      * </ol>
 109      */
 110     public static final NameClass NULL = new NullNameClass();
 111 }