Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
vtkXMLMaterialParser.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 /*=========================================================================
18 
19  Program: Visualization Toolkit
20  Module: vtkXMLMaterialParser.cxx
21 
22  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  All rights reserved.
24  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
25 
26  This software is distributed WITHOUT ANY WARRANTY; without even
27  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
28  PURPOSE. See the above copyright notice for more information.
29 
30 =========================================================================*/
31 
32 /*
33  * Copyright 2003 Sandia Corporation.
34  * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
35  * license for use of this work by or on behalf of the
36  * U.S. Government. Redistribution and use in source and binary forms, with
37  * or without modification, are permitted provided that this Notice and any
38  * statement of authorship are reproduced on all copies.
39  */
40 
41 #include "vtkXMLMaterialParser.h"
42 
43 #include "vtkObjectFactory.h"
44 #include "vtkSmartPointer.h"
45 #include "vtkXMLDataElement.h"
46 #include "vtkXMLMaterial.h"
47 
48 #include "vtkXMLUtilities.h"
49 
50 #include <vector>
51 
52 //-----------------------------------------------------------------------------
53 class vtkXMLMaterialParserInternals
54 {
55 public:
56  typedef std::vector<vtkSmartPointer<vtkXMLDataElement>> VectorOfElements;
57  VectorOfElements Stack;
58 };
59 
60 //-----------------------------------------------------------------------------
63 
64 //-----------------------------------------------------------------------------
66 {
67  this->Material = vtkXMLMaterial::New();
68  this->Material->Register(this);
69  this->Material->Delete();
70  this->Internals = new vtkXMLMaterialParserInternals;
71 }
72 
73 //-----------------------------------------------------------------------------
75 {
76  delete this->Internals;
77  this->SetMaterial(nullptr);
78 }
79 
80 //-----------------------------------------------------------------------------
81 int vtkXMLMaterialParser::Parse(const char *str)
82 {
83  return this->Superclass::Parse(str);
84 }
85 
86 //-----------------------------------------------------------------------------
87 int vtkXMLMaterialParser::Parse(const char *str, unsigned int length)
88 {
89  return this->Superclass::Parse(str, length);
90 }
91 
92 //-----------------------------------------------------------------------------
94 {
95  this->Internals->Stack.clear();
96  return this->Superclass::Parse();
97 }
98 
99 //-----------------------------------------------------------------------------
101 {
102  int ret = this->Superclass::InitializeParser();
103  if (ret)
104  {
105  this->Internals->Stack.clear();
106  }
107  return ret;
108 }
109 
110 //-----------------------------------------------------------------------------
111 void vtkXMLMaterialParser::StartElement(const char *name, const char **atts)
112 {
113  vtkXMLDataElement *element = vtkXMLDataElement::New();
114  element->SetName(name);
115  element->SetXMLByteIndex(this->GetXMLByteIndex());
116  vtkXMLUtilities::ReadElementFromAttributeArray(element, atts, VTK_ENCODING_NONE);
117  const char *id = element->GetAttribute("id");
118  if (id)
119  {
120  element->SetId(id);
121  }
122  this->Internals->Stack.push_back(element);
123  element->Delete();
124 }
125 
126 //-----------------------------------------------------------------------------
127 void vtkXMLMaterialParser::EndElement(const char *vtkNotUsed(name))
128 {
129  vtkXMLDataElement *finished = this->Internals->Stack.back().GetPointer();
130  int prev_pos = static_cast<int>(this->Internals->Stack.size()) - 2;
131  if (prev_pos >= 0)
132  {
133  this->Internals->Stack[prev_pos].GetPointer()->AddNestedElement(finished);
134  }
135  else
136  {
137  this->Material->SetRootElement(finished);
138  }
139 
140  this->Internals->Stack.pop_back();
141 }
142 
143 //-----------------------------------------------------------------------------
144 void vtkXMLMaterialParser::CharacterDataHandler(const char *inData, int inLength)
145 {
146  if (this->Internals->Stack.size() > 0)
147  {
148  vtkXMLDataElement *elem = this->Internals->Stack.back().GetPointer();
149  elem->AddCharacterData(inData, inLength);
150  }
151  /*
152  // this wont happen as the XML parser will flag it as an error.
153  else
154  {
155  vtkErrorMacro("Character data not enclosed in XML tags");
156  }
157  */
158 }
159 
160 //-----------------------------------------------------------------------------
161 void vtkXMLMaterialParser::PrintSelf(ostream &os, vtkIndent indent)
162 {
163  this->Superclass::PrintSelf(os, indent);
164  os << indent << "Material: ";
165  this->Material->PrintSelf(os, indent.GetNextIndent());
166 }
vtkXMLMaterialParserInternals * Internals
virtual void CharacterDataHandler(const char *data, int length) override
virtual void EndElement(const char *) override
void SetMaterial(vtkXMLMaterial *)
static vtkXMLMaterial * New()
void PrintSelf(ostream &os, vtkIndent indent) override
virtual void StartElement(const char *name, const char **atts) override
vtkCxxSetObjectMacro(vtkXMLMaterialParser, Material, vtkXMLMaterial)
virtual int InitializeParser() override
virtual int Parse() override
void SetRootElement(vtkXMLDataElement *)
void PrintSelf(ostream &os, vtkIndent indent) override
vtkStandardNewMacro(vtkXMLMaterialParser)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.