View Javadoc
1   /**
2    * Copyright (C) 2014-2018 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 javax.annotation.Nonnull;
20  import javax.annotation.Nullable;
21  import javax.annotation.concurrent.NotThreadSafe;
22  
23  import com.helger.commons.ValueEnforcer;
24  import com.helger.commons.annotation.ReturnsMutableCopy;
25  import com.helger.commons.collection.impl.CommonsHashMap;
26  import com.helger.commons.collection.impl.CommonsTreeSet;
27  import com.helger.commons.collection.impl.ICommonsMap;
28  import com.helger.commons.collection.impl.ICommonsSortedSet;
29  import com.helger.commons.string.ToStringGenerator;
30  import com.helger.schematron.pure.model.PSPattern;
31  import com.helger.schematron.pure.model.PSRule;
32  import com.helger.schematron.pure.model.PSSchema;
33  
34  /**
35   * Utility lookup cache for ID to pattern and ID to rule, to avoid the linear
36   * access when scanning a schema or a pattern. This cache only contains abstract
37   * patterns and rules.
38   *
39   * @author Philip Helger
40   */
41  @NotThreadSafe
42  final class PreprocessorLookup
43  {
44    private final ICommonsMap <String, PSPattern> m_aPatterns = new CommonsHashMap <> ();
45    private final ICommonsMap <String, PSRule> m_aRules = new CommonsHashMap <> ();
46  
47    public PreprocessorLookup (@Nonnull final PSSchema aSchema)
48    {
49      ValueEnforcer.notNull (aSchema, "Schema");
50  
51      for (final PSPattern aPattern : aSchema.getAllPatterns ())
52      {
53        // Only handle abstract patterns
54        if (aPattern.isAbstract ())
55          m_aPatterns.put (aPattern.getID (), aPattern);
56  
57        for (final PSRule aRule : aPattern.getAllRules ())
58        {
59          // Only handle abstract rules
60          if (aRule.isAbstract ())
61            m_aRules.put (aRule.getID (), aRule);
62        }
63      }
64    }
65  
66    /**
67     * Get the abstract pattern with the specified ID.
68     *
69     * @param sID
70     *        The pattern ID to search. May be <code>null</code>.
71     * @return <code>null</code> if no such abstract pattern is contained.
72     */
73    @Nullable
74    public PSPattern getAbstractPatternOfID (@Nullable final String sID)
75    {
76      return m_aPatterns.get (sID);
77    }
78  
79    /**
80     * Get the abstract rule with the specified ID.
81     *
82     * @param sID
83     *        The rule ID to search. May be <code>null</code>.
84     * @return <code>null</code> if no such abstract rule is contained.
85     */
86    @Nullable
87    public PSRule getAbstractRuleOfID (@Nullable final String sID)
88    {
89      return m_aRules.get (sID);
90    }
91  
92    /**
93     * @return A sorted set with all abstract rules IDs present.
94     */
95    @Nonnull
96    @ReturnsMutableCopy
97    public ICommonsSortedSet <String> getAllAbstractRuleIDs ()
98    {
99      return new CommonsTreeSet <> (m_aRules.keySet ());
100   }
101 
102   @Override
103   public String toString ()
104   {
105     return new ToStringGenerator (this).append ("patterns", m_aPatterns).append ("rules", m_aRules).getToString ();
106   }
107 }