23 xercesc::XMLPlatformUtils::Initialize();
33 G4String preFatalCode;
40 xercesc::XercesDOMParser parser;
41 parser.setDoNamespaces(
false);
42 parser.setDoSchema(
false);
43 parser.setCreateEntityReferenceNodes(
false);
46 parser.parse(path.c_str());
47 }
catch (
const xercesc::XMLException& e) {
48 preFatalCode =
"G4OCCT_MatReader001";
49 preFatalMsg =
"XML error parsing '" + path +
"': " + Transcode(e.getMessage());
51 }
catch (
const xercesc::DOMException& e) {
52 preFatalCode =
"G4OCCT_MatReader002";
53 preFatalMsg =
"DOM error parsing '" + path +
"': " + Transcode(e.getMessage());
57 const xercesc::DOMDocument*
const doc = parser.getDocument();
59 preFatalCode =
"G4OCCT_MatReader003";
60 preFatalMsg =
"Cannot open document: " + path;
64 const xercesc::DOMElement*
const root = doc->getDocumentElement();
66 preFatalCode =
"G4OCCT_MatReader004";
67 preFatalMsg =
"Empty document: " + path;
71 const G4String rootTag = Transcode(root->getTagName());
72 if (rootTag !=
"materials") {
73 preFatalCode =
"G4OCCT_MatReader005";
74 preFatalMsg =
"Root element must be <materials>, got <" + rootTag +
"> in '" + path +
"'";
80 for (xercesc::DOMNode* node = root->getFirstChild(); node !=
nullptr;
81 node = node->getNextSibling()) {
82 if (node->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) {
85 const auto* child =
dynamic_cast<const xercesc::DOMElement*
>(node);
89 const G4String tag = Transcode(child->getTagName());
90 if (tag ==
"isotope") {
92 }
else if (tag ==
"element") {
98 for (xercesc::DOMNode* node = root->getFirstChild(); node !=
nullptr;
99 node = node->getNextSibling()) {
100 if (node->getNodeType() != xercesc::DOMNode::ELEMENT_NODE) {
103 const auto* child =
dynamic_cast<const xercesc::DOMElement*
>(node);
107 if (Transcode(child->getTagName()) !=
"material") {
115 const xercesc::DOMNamedNodeMap* attrs = child->getAttributes();
116 for (XMLSize_t i = 0; i < attrs->getLength(); ++i) {
117 const auto* attr =
dynamic_cast<const xercesc::DOMAttr*
>(attrs->item(i));
121 const G4String aName = Transcode(attr->getName());
122 const G4String aValue = Transcode(attr->getValue());
123 if (aName ==
"stepName") {
125 }
else if (aName ==
"geant4Name") {
127 }
else if (aName ==
"name") {
132 if (stepName.empty()) {
133 G4Exception(
"G4OCCTMaterialMapReader::ReadFile",
"G4OCCT_MatReader006", FatalException,
134 (
"<material> element in '" + path +
135 "' is missing the required 'stepName' "
141 G4Material* mat =
nullptr;
143 if (!geant4Name.empty()) {
145 mat = G4NistManager::Instance()->FindOrBuildMaterial(geant4Name);
147 G4Exception(
"G4OCCTMaterialMapReader::ReadFile",
"G4OCCT_MatReader007", FatalException,
148 (
"NIST material '" + geant4Name +
"' not found (stepName='" + stepName +
150 "Check the spelling against the Geant4 NIST material database.")
156 if (gdmlName.empty()) {
157 G4Exception(
"G4OCCTMaterialMapReader::ReadFile",
"G4OCCT_MatReader008", FatalException,
158 (
"Inline <material stepName=\"" + stepName +
"\"> in '" + path +
159 "' requires a 'name' attribute (used as the GDML material registry key).")
173 mat = G4Material::GetMaterial(GenerateName(gdmlName),
false);
174 if (mat ==
nullptr) {
175 mat = G4Material::GetMaterial(gdmlName,
false);
177 if (mat ==
nullptr) {
181 mat = GetMaterial(GenerateName(gdmlName));
182 if (mat ==
nullptr) {
183 mat = G4Material::GetMaterial(gdmlName,
false);
187 G4Exception(
"G4OCCTMaterialMapReader::ReadFile",
"G4OCCT_MatReader009", FatalException,
188 (
"Failed to create inline material '" + gdmlName +
"' (stepName='" +
189 stepName +
"') in '" + path +
190 "'. Check that density and composition are valid.")
196 result.
Add(stepName, mat);
200 xercesc::XMLPlatformUtils::Terminate();
202 if (!preFatalCode.empty()) {
203 G4Exception(
"G4OCCTMaterialMapReader::ReadFile", preFatalCode.c_str(), FatalException,
204 preFatalMsg.c_str());