1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef TX_TXSTYLESHEET_H
7 #define TX_TXSTYLESHEET_H
8 
9 #include "txOutputFormat.h"
10 #include "txExpandedNameMap.h"
11 #include "txList.h"
12 #include "txXSLTPatterns.h"
13 #include "nsISupportsImpl.h"
14 
15 class txInstruction;
16 class txTemplateItem;
17 class txVariableItem;
18 class txStripSpaceItem;
19 class txAttributeSetItem;
20 class txDecimalFormat;
21 class txStripSpaceTest;
22 class txXSLKey;
23 
24 class txStylesheet final {
25  public:
26   class ImportFrame;
27   class GlobalVariable;
28   friend class txStylesheetCompilerState;
29   // To be able to do some cleaning up in destructor
30   friend class ImportFrame;
31 
32   txStylesheet();
33   nsresult init();
34 
35   NS_INLINE_DECL_REFCOUNTING(txStylesheet)
36 
37   nsresult findTemplate(const txXPathNode& aNode, const txExpandedName& aMode,
38                         txIMatchContext* aContext, ImportFrame* aImportedBy,
39                         txInstruction** aTemplate, ImportFrame** aImportFrame);
40   txDecimalFormat* getDecimalFormat(const txExpandedName& aName);
41   txInstruction* getAttributeSet(const txExpandedName& aName);
42   txInstruction* getNamedTemplate(const txExpandedName& aName);
43   txOutputFormat* getOutputFormat();
44   GlobalVariable* getGlobalVariable(const txExpandedName& aName);
45   const txOwningExpandedNameMap<txXSLKey>& getKeyMap();
46   nsresult isStripSpaceAllowed(const txXPathNode& aNode,
47                                txIMatchContext* aContext, bool& aAllowed);
48 
49   /**
50    * Called by the stylesheet compiler once all stylesheets has been read.
51    */
52   nsresult doneCompiling();
53 
54   /**
55    * Add a key to the stylesheet
56    */
57   nsresult addKey(const txExpandedName& aName,
58                   mozilla::UniquePtr<txPattern> aMatch,
59                   mozilla::UniquePtr<Expr> aUse);
60 
61   /**
62    * Add a decimal-format to the stylesheet
63    */
64   nsresult addDecimalFormat(const txExpandedName& aName,
65                             mozilla::UniquePtr<txDecimalFormat>&& aFormat);
66 
67   struct MatchableTemplate {
68     txInstruction* mFirstInstruction;
69     mozilla::UniquePtr<txPattern> mMatch;
70     double mPriority;
71   };
72 
73   /**
74    * Contain information that is import precedence dependant.
75    */
76   class ImportFrame {
77    public:
ImportFrame()78     ImportFrame() : mFirstNotImported(nullptr) {}
79     ~ImportFrame();
80 
81     // List of toplevel items
82     txList mToplevelItems;
83 
84     // Map of template modes
85     txOwningExpandedNameMap<nsTArray<MatchableTemplate> > mMatchableTemplates;
86 
87     // ImportFrame which is the first one *not* imported by this frame
88     ImportFrame* mFirstNotImported;
89   };
90 
91   class GlobalVariable : public txObject {
92    public:
93     GlobalVariable(mozilla::UniquePtr<Expr>&& aExpr,
94                    mozilla::UniquePtr<txInstruction>&& aFirstInstruction,
95                    bool aIsParam);
96 
97     mozilla::UniquePtr<Expr> mExpr;
98     mozilla::UniquePtr<txInstruction> mFirstInstruction;
99     bool mIsParam;
100   };
101 
102  private:
103   // Private destructor, to discourage deletion outside of Release():
104   ~txStylesheet();
105 
106   nsresult addTemplate(txTemplateItem* aTemplate, ImportFrame* aImportFrame);
107   nsresult addGlobalVariable(txVariableItem* aVariable);
108   nsresult addFrames(txListIterator& aInsertIter);
109   nsresult addStripSpace(txStripSpaceItem* aStripSpaceItem,
110                          nsTArray<txStripSpaceTest*>& aFrameStripSpaceTests);
111   nsresult addAttributeSet(txAttributeSetItem* aAttributeSetItem);
112 
113   // List of ImportFrames
114   txList mImportFrames;
115 
116   // output format
117   txOutputFormat mOutputFormat;
118 
119   // List of first instructions of templates. This is the owner of all
120   // instructions used in templates
121   txList mTemplateInstructions;
122 
123   // Root importframe
124   ImportFrame* mRootFrame;
125 
126   // Named templates
127   txExpandedNameMap<txInstruction> mNamedTemplates;
128 
129   // Map with all decimal-formats
130   txOwningExpandedNameMap<txDecimalFormat> mDecimalFormats;
131 
132   // Map with all named attribute sets
133   txExpandedNameMap<txInstruction> mAttributeSets;
134 
135   // Map with all global variables and parameters
136   txOwningExpandedNameMap<GlobalVariable> mGlobalVariables;
137 
138   // Map with all keys
139   txOwningExpandedNameMap<txXSLKey> mKeys;
140 
141   // Array of all txStripSpaceTests, sorted in acending order
142   nsTArray<mozilla::UniquePtr<txStripSpaceTest> > mStripSpaceTests;
143 
144   // Default templates
145   mozilla::UniquePtr<txInstruction> mContainerTemplate;
146   mozilla::UniquePtr<txInstruction> mCharactersTemplate;
147   mozilla::UniquePtr<txInstruction> mEmptyTemplate;
148 };
149 
150 /**
151  * txStripSpaceTest holds both an txNameTest and a bool for use in
152  * whitespace stripping.
153  */
154 class txStripSpaceTest {
155  public:
txStripSpaceTest(nsAtom * aPrefix,nsAtom * aLocalName,int32_t aNSID,bool stripSpace)156   txStripSpaceTest(nsAtom* aPrefix, nsAtom* aLocalName, int32_t aNSID,
157                    bool stripSpace)
158       : mNameTest(aPrefix, aLocalName, aNSID, txXPathNodeType::ELEMENT_NODE),
159         mStrips(stripSpace) {}
160 
matches(const txXPathNode & aNode,txIMatchContext * aContext,bool & aMatched)161   nsresult matches(const txXPathNode& aNode, txIMatchContext* aContext,
162                    bool& aMatched) {
163     return mNameTest.matches(aNode, aContext, aMatched);
164   }
165 
stripsSpace()166   bool stripsSpace() { return mStrips; }
167 
getDefaultPriority()168   double getDefaultPriority() { return mNameTest.getDefaultPriority(); }
169 
170  protected:
171   txNameTest mNameTest;
172   bool mStrips;
173 };
174 
175 /**
176  * Value of a global parameter
177  */
178 class txIGlobalParameter {
179  public:
180   MOZ_COUNTED_DEFAULT_CTOR(txIGlobalParameter)
181   MOZ_COUNTED_DTOR_VIRTUAL(txIGlobalParameter)
182   virtual nsresult getValue(txAExprResult** aValue) = 0;
183 };
184 
185 #endif  // TX_TXSTYLESHEET_H
186