source: trunk/source/persistency/gdml/src/G4GDMLBase.cc @ 818

Last change on this file since 818 was 818, checked in by garnier, 16 years ago

import all except CVS

File size: 5.2 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4GDMLBase.cc,v 1.3.2.1 2008/01/16 09:43:30 ztorzsok Exp $
28// GEANT4 tag $Name: geant4-09-01-patch-02 $
29//
30//
31// class G4GDMLBase
32//
33// Class description:
34//
35// History:
36// - Created.                                  Zoltan Torzsok, November 2007
37// -------------------------------------------------------------------------
38
39#include "G4GDMLBase.hh"
40
41G4GDMLBase::G4GDMLBase() {
42
43   try {
44
45      xercesc::XMLPlatformUtils::Initialize();
46   }
47   catch(xercesc::XMLException& e) {
48
49      char* message = xercesc::XMLString::transcode(e.getMessage());
50      G4cerr << "XML toolkit initialization error: " << message << G4endl;
51      xercesc::XMLString::release(&message);
52   }
53
54   parser = new xercesc::XercesDOMParser;
55
56   parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
57   parser->setDoNamespaces(true);
58   parser->setDoSchema(true);
59   parser->setValidationSchemaFullChecking(true);
60   parser->setCreateEntityReferenceNodes(false);   // Entities will be automatically resolved by Xerces
61}
62
63G4GDMLBase::~G4GDMLBase() {
64
65   if (parser) delete parser;
66
67   try {
68
69      xercesc::XMLPlatformUtils::Terminate();
70   }
71   catch(xercesc::XMLException& e) {
72   
73      char* message = xercesc::XMLString::transcode(e.getMessage());
74      G4cerr << "XML toolkit termination error: " << message << G4endl;
75      xercesc::XMLString::release(&message);
76   }
77}
78
79G4String G4GDMLBase::GenerateName(const G4String& in) {
80
81   std::string out(prename);
82   
83   std::string::size_type open = in.find("[",0);
84
85   out.append(in,0,open);
86   
87   while (open != std::string::npos) {
88   
89      std::string::size_type close = in.find("]",open);
90
91      if (close == std::string::npos) G4Exception("Bracket mismatch in loop!");
92   
93      std::string expr = in.substr(open+1,close-open-1);
94
95      std::stringstream stream;
96     
97      stream << "[" << eval.EvaluateInteger(expr) << "]";
98   
99      out.append(stream.str());
100
101      open = in.find("[",close);
102   }
103
104   return out;
105}
106
107void G4GDMLBase::Parse(const G4String& fileName) {
108
109   prename = fileName + "_";
110
111   try {
112
113      parser->parse(fileName.c_str());
114   }
115   catch (const xercesc::XMLException &e) {
116   
117      char* message = xercesc::XMLString::transcode(e.getMessage());
118      G4cout << "XML: " << message << G4endl;
119      xercesc::XMLString::release(&message);
120   }
121   catch (const xercesc::DOMException &e) {
122   
123      char* message = xercesc::XMLString::transcode(e.getMessage());
124      G4cout << "DOM: " << message << G4endl;
125      xercesc::XMLString::release(&message);
126   }
127
128   xercesc::DOMDocument* doc = parser->getDocument();
129
130   if (!doc) G4Exception("GDML: Unable to open document: "+fileName);
131
132   xercesc::DOMElement* element = doc->getDocumentElement();
133
134   if (!element) G4Exception("GDML: Empty document!");
135
136   for (xercesc::DOMNode* iter = element->getFirstChild();iter != 0;iter = iter->getNextSibling()) {
137
138      if (iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) continue;
139
140      const xercesc::DOMElement* const child = dynamic_cast<xercesc::DOMElement*>(iter);
141
142      const G4String tag = xercesc::XMLString::transcode(child->getTagName());
143
144      if (tag=="define"   ) defineRead(child); else
145      if (tag=="materials") materialsRead(child); else
146      if (tag=="solids"   ) solidsRead(child); else
147      if (tag=="setup"    ) setupRead(child); else
148      if (tag=="structure") structureRead(child);
149   }
150}
151
152G4PVPlacement* G4GDMLBase::getTopVolume(const G4String& setupName) {
153
154   G4LogicalVolume* volume = getVolume(getSetup(setupName));
155
156   volume->SetVisAttributes(G4VisAttributes::Invisible);
157
158   return new G4PVPlacement(0,G4ThreeVector(),volume,"",0,0,0);
159}
Note: See TracBrowser for help on using the repository browser.