001    package com.oxygenxml.validate.nvdl;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    import java.util.StringTokenizer;
006    
007    import org.apache.xerces.util.XMLChar;
008    
009    /**
010     * Strores trigger information and checks trigger activation.
011     * @author george
012     */
013    public class Trigger {
014      /**
015       * The namespace name for the local element names.
016       */
017      String namespace;
018      /**
019       * List with local names.
020       */
021      List elementNames;
022      
023      /**
024       * Stores the invalid local names if any, otherwise null.
025       */
026      String errors;
027      
028      /**
029       * Creates a trigger to store the elements that break sections
030       * for a given namespace.
031       * @param namespace The namespace
032       * @param nameList A space separated list of local names.
033       */
034      Trigger(String namespace, String nameList) {
035        StringTokenizer st = new StringTokenizer(nameList);
036        elementNames = new ArrayList(st.countTokens());
037        while (st.hasMoreTokens()) {
038          String name = st.nextToken();
039          elementNames.add(name);
040          if (!XMLChar.isValidNCName(name)) {
041            if (errors == null) {
042              errors = name;
043            } else {
044              errors += (" " + name);
045            }
046          }
047        }
048        this.namespace = namespace;
049      }
050    
051      /**
052       * Checks trigger activation given an element name and its parent, 
053       * both from the same namespace.
054       * @param ns The namespace for the elements.
055       * @param name The current element local name.
056       * @param parent The parent element local name.
057       * @return true if we should brake the section.
058       */
059      boolean trigger(String ns, String name, String parent) {
060        if ((ns==null && namespace==null) || (ns!=null && ns.equals(namespace))) {
061          if (elementNames.contains(name)) {
062            return (!elementNames.contains(parent));
063          }
064        }
065            return false;
066      }
067    }