View Javadoc
1   /**
2    * Copyright (C) 2014-2015 Philip Helger (www.helger.com)
3    * philip[at]helger[dot]com
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *         http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package com.helger.schematron.pure.preprocess;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.SortedSet;
22  
23  import javax.annotation.Nonnull;
24  import javax.annotation.Nullable;
25  import javax.annotation.concurrent.NotThreadSafe;
26  
27  import com.helger.commons.ValueEnforcer;
28  import com.helger.commons.annotation.ReturnsMutableCopy;
29  import com.helger.commons.collection.CollectionHelper;
30  import com.helger.commons.string.ToStringGenerator;
31  import com.helger.schematron.pure.model.PSPattern;
32  import com.helger.schematron.pure.model.PSRule;
33  import com.helger.schematron.pure.model.PSSchema;
34  
35  /**
36   * Utility lookup cache for ID to pattern and ID to rule, to avoid the linear
37   * access when scanning a schema or a pattern. This cache only contains abstract
38   * patterns and rules.
39   *
40   * @author Philip Helger
41   */
42  @NotThreadSafe
43  final class PreprocessorLookup
44  {
45    private final Map <String, PSPattern> m_aPatterns = new HashMap <String, PSPattern> ();
46    private final Map <String, PSRule> m_aRules = new HashMap <String, PSRule> ();
47  
48    public PreprocessorLookup (@Nonnull final PSSchema aSchema)
49    {
50      ValueEnforcer.notNull (aSchema, "Schema");
51  
52      for (final PSPattern aPattern : aSchema.getAllPatterns ())
53      {
54        // Only handle abstract patterns
55        if (aPattern.isAbstract ())
56          m_aPatterns.put (aPattern.getID (), aPattern);
57  
58        for (final PSRule aRule : aPattern.getAllRules ())
59        {
60          // Only handle abstract rules
61          if (aRule.isAbstract ())
62            m_aRules.put (aRule.getID (), aRule);
63        }
64      }
65    }
66  
67    /**
68     * Get the abstract pattern with the specified ID.
69     *
70     * @param sID
71     *        The pattern ID to search. May be <code>null</code>.
72     * @return <code>null</code> if no such abstract pattern is contained.
73     */
74    @Nullable
75    public PSPattern getAbstractPatternOfID (@Nullable final String sID)
76    {
77      return m_aPatterns.get (sID);
78    }
79  
80    /**
81     * Get the abstract rule with the specified ID.
82     *
83     * @param sID
84     *        The rule ID to search. May be <code>null</code>.
85     * @return <code>null</code> if no such abstract rule is contained.
86     */
87    @Nullable
88    public PSRule getAbstractRuleOfID (@Nullable final String sID)
89    {
90      return m_aRules.get (sID);
91    }
92  
93    /**
94     * @return A sorted set with all abstract rules IDs present.
95     */
96    @Nonnull
97    @ReturnsMutableCopy
98    public SortedSet <String> getAllAbstractRuleIDs ()
99    {
100     return CollectionHelper.newSortedSet (m_aRules.keySet ());
101   }
102 
103   @Override
104   public String toString ()
105   {
106     return new ToStringGenerator (this).append ("patterns", m_aPatterns).append ("rules", m_aRules).toString ();
107   }
108 }