1 /*=========================================================================
2 
3   Program:   ParaView
4   Module:    vtkExodusIIReaderParser.h
5 
6   Copyright (c) Kitware, Inc.
7   All rights reserved.
8   See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /**
16  * @class   vtkExodusIIReaderParser
17  * @brief   internal parser used by vtkExodusIIReader.
18  *
19  * vtkExodusIIReaderParser is an internal XML parser used by vtkExodusIIReader.
20  * This is not for public use.
21  */
22 
23 #ifndef vtkExodusIIReaderParser_h
24 #define vtkExodusIIReaderParser_h
25 
26 #include "vtkIOExodusModule.h" // For export macro
27 #include "vtkSmartPointer.h"   // for ivars
28 #include "vtkXMLParser.h"
29 
30 #include <map>    // for std::map
31 #include <set>    // for std::set
32 #include <string> // for std::string
33 #include <vector> // for std::vector
34 
35 class vtkMutableDirectedGraph;
36 class vtkStringArray;
37 class vtkUnsignedCharArray;
38 
39 class VTKIOEXODUS_EXPORT vtkExodusIIReaderParser : public vtkXMLParser
40 {
41 public:
42   static vtkExodusIIReaderParser* New();
43   vtkTypeMacro(vtkExodusIIReaderParser, vtkXMLParser);
44   void PrintSelf(ostream& os, vtkIndent indent) override;
45 
46   ///@{
47   /**
48    * Returns the SIL.
49    * This is valid only after Go().
50    */
51   vtkGetObjectMacro(SIL, vtkMutableDirectedGraph);
52   ///@}
53 
54   /**
55    * Trigger parsing of the XML file.
56    */
57   void Go(VTK_FILEPATH const char* filename);
58 
59   // Returns if the parser has some information about the block with given "id".
60   // This is valid only after Go().
HasInformationAboutBlock(int id)61   bool HasInformationAboutBlock(int id)
62   {
63     return (this->BlockID_To_VertexID.find(id) != this->BlockID_To_VertexID.end());
64   }
65 
66   /**
67    * Given a block "id" return the name as determined from the xml.
68    * This is valid only after Go().
69    */
70   std::string GetBlockName(int id);
71 
72   ///@{
73   /**
74    * Fills up the blockIdsSet with the block ids referred to by the XML.
75    * This is valid only after Go().
76    */
GetBlockIds(std::set<int> & blockIdsSet)77   void GetBlockIds(std::set<int>& blockIdsSet)
78   {
79     std::map<int, vtkIdType>::iterator iter;
80     for (iter = this->BlockID_To_VertexID.begin(); iter != this->BlockID_To_VertexID.end(); ++iter)
81     {
82       blockIdsSet.insert(iter->first);
83     }
84   }
85   ///@}
86 
87 protected:
88   vtkExodusIIReaderParser();
89   ~vtkExodusIIReaderParser() override;
90 
91   void StartElement(const char* tagName, const char** attrs) override;
92   void EndElement(const char* tagName) override;
93   void FinishedParsing();
94 
GetValue(const char * attr,const char ** attrs)95   const char* GetValue(const char* attr, const char** attrs)
96   {
97     int i;
98     for (i = 0; attrs[i]; i += 2)
99     {
100       const char* name = strrchr(attrs[i], ':');
101       if (!name)
102       {
103         name = attrs[i];
104       }
105       else
106       {
107         name++;
108       }
109       if (strcmp(attr, name) == 0)
110       {
111         return attrs[i + 1];
112       }
113     }
114     return nullptr;
115   }
116 
117   // Convenience methods to add vertices/edges to the SIL.
118   vtkIdType AddVertexToSIL(const char* name);
119   vtkIdType AddChildEdgeToSIL(vtkIdType src, vtkIdType dst);
120   vtkIdType AddCrossEdgeToSIL(vtkIdType src, vtkIdType dst);
121 
122   /**
123    * Returns the vertex id for the "part" with given
124    * part_number_instance_string formed as
125    * "{part-number} Instance: {part-instance}"
126    */
127   vtkIdType GetPartVertex(const char* part_number_instance_string);
128 
129   // For each of the blocks, this maps the "id" attribute in the XML to the
130   // vertex id for the block in the SIL.
131   std::map<int, vtkIdType> BlockID_To_VertexID;
132 
133   // Maps block "id"s to material names.
134   std::map<int, std::string> BlockID_To_MaterialName;
135 
136   // Maps material name to vertex id.
137   // This will be build only if <material-list> is present in the XML.
138   std::map<std::string, vtkIdType> MaterialName_To_VertexID;
139 
140   std::map<vtkIdType, std::string> PartVertexID_To_Descriptions;
141 
142   // These save the values read from <material-specification /> element present
143   // withint the <part /> elements.
144   // key: part vertex id
145   // value: material name = (desp + spec)
146   std::map<vtkIdType, std::string> MaterialSpecifications;
147 
148   // Maps the "{part-number} Instance: {part-instance}" key for the vertex id
149   // for the part vertex in the Assemblies hierarchy.
150   std::map<std::string, vtkIdType> Part_To_VertexID;
151 
152   // Maps a block-id to the "{part-number} Instance: {part-instance}" string.
153   std::map<int, std::string> BlockID_To_Part;
154 
155   vtkMutableDirectedGraph* SIL;
156   vtkSmartPointer<vtkStringArray> NamesArray;
157   vtkSmartPointer<vtkUnsignedCharArray> CrossEdgesArray;
158 
159   std::string BlockPartNumberString;
160 
161   vtkIdType RootVertex;
162   vtkIdType BlocksVertex;
163   vtkIdType AssembliesVertex;
164   vtkIdType MaterialsVertex;
165   std::vector<vtkIdType> CurrentVertex;
166 
167   bool InBlocks;
168   bool InMaterialAssignments;
169 
170 private:
171   vtkExodusIIReaderParser(const vtkExodusIIReaderParser&) = delete;
172   void operator=(const vtkExodusIIReaderParser&) = delete;
173 };
174 
175 #endif
176