00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "AMLReader.hpp"
00026
00027 using namespace BasicUtilities;
00028 using namespace std;
00029
00030 AMLReader::AMLReader() {}
00031
00032 AMLReader::~AMLReader() {}
00033
00034
00035 UAPNode* AMLReader::AMLFileToAMLRep (const string& file_name) {
00036
00037
00038 UAPNode* root = new UAPNode("UAP_root");
00039
00040
00041 UAPNode* aml_rep = root->addChild ("AML_representation");
00042 aml_rep->addAttribute ("source_file_language", "AML");
00043 aml_rep->addAttribute ("filename", file_name);
00044
00045
00046 FileStackList file_stack;
00047
00048 if (!read_file (aml_rep, file_name, file_stack)) return NULL;
00049
00050 return root;
00051
00052 }
00053
00054
00055
00056
00057
00058 bool AMLReader::read_file (UAPNode* input_node, string file_name, FileStackList file_stack) {
00059
00060 info_out.ix_line = -1;
00061 info_out.parsing_status = "Opening a lattice file";
00062
00063
00064
00065 file_stack.addFile(file_name);
00066 string this_file = file_stack.file_list.back().full_name;
00067
00068
00069
00070 ifstream myfile;
00071 myfile.open(this_file.c_str());
00072 if (!myfile.is_open()) {
00073 info_out.error ("File not found: " + file_name);
00074 return false;
00075 }
00076 myfile.close();
00077
00078
00079
00080 SAX2Wrapper parser;
00081 if (!parser.XMLFileToUAPRep(this_file, input_node)) return false;
00082
00083
00084
00085 NodeVec inc_list = input_node->getSubNodesByName ("{http://www.w3.org/2001/XInclude}include");
00086 for (NodeVecIter it=inc_list.begin(); it!=inc_list.end(); ++it) {
00087 UAPNode* include_node = *it;
00088 string include_file = include_node->getAttributeString("href");
00089
00090 cout << "Including: " + include_file << endl;
00091 if (!read_file (include_node, include_file, file_stack)) return false;
00092
00093
00094
00095 UAPNode* null_node = include_node->getChildByName("null_root");
00096 if (null_node && include_node->getChildren().size() == 1) {
00097 NodeVec children = null_node->getChildren();
00098 for (NodeVecIter ic = children.begin(); ic != children.end(); ic++)
00099 include_node->addChildCopy(*ic);
00100 null_node->deleteNode();
00101 }
00102
00103 }
00104
00105 return true;
00106
00107 }
00108
00109
00110
00111
00112 UAPNode* AMLReader::AMLRepToAMLFile (UAPNode* aml_root,
00113 const string& file_name, bool one_file) {
00114
00115 info_out.ix_line = -1;
00116 info_out.parsing_status = "Creating an AML lattice file";
00117
00118 UAPNode* aml_rep = aml_root;
00119
00120 if (aml_rep->getName() == "UAP_root")
00121 aml_rep = aml_rep->getChildByName("AML_representation");
00122
00123 if (!aml_rep || (aml_rep->getName() != "AML_representation")) {
00124 info_out.error ("NODE argument is not <AML_representation> or its parent");
00125 return NULL;
00126 }
00127
00128 UAPNode* lab = aml_rep->getChildByName("laboratory");
00129
00130 if (!lab || lab->getName() != "laboratory") {
00131 info_out.error ("Node argument is not <laboratory> or its parent");
00132 return NULL;
00133 }
00134
00135
00136 ofstream* aml_out = NULL;
00137 string this_file = file_name;
00138 if (this_file == "") this_file = aml_rep->getAttributeString("filename");
00139 if (this_file == "") {
00140 info_out.error ("No file name given for creating an AML file.");
00141 return NULL;
00142 }
00143 aml_out = new ofstream(this_file.c_str());
00144 if (!aml_out->is_open()) {
00145 info_out.error ("Cannot open file: " + this_file);
00146 return NULL;
00147 }
00148
00149 FileStackList file_stack;
00150 file_stack.addFile(this_file);
00151 aml_rep_to_aml_file (lab, one_file, aml_out, file_stack);
00152
00153 if (aml_out) {
00154 if (aml_out->is_open()) aml_out->close();
00155 delete aml_out;
00156 }
00157
00158 return aml_rep;
00159
00160 }
00161
00162
00163
00164
00165 bool AMLReader::aml_rep_to_aml_file (UAPNode* aml_rep, bool one_file,
00166 ofstream* aml_output, FileStackList file_stack, int indent) {
00167
00168 bool ok = true;
00169
00170
00171
00172 ofstream* aml_out;
00173 NodeVec children = aml_rep->getChildren();
00174 string node_name = aml_rep->getQName();
00175
00176
00177
00178 *aml_output << string(indent, ' ') << "<" << node_name;
00179
00180
00181
00182 AttributeVec attributes = aml_rep->getAttributes();
00183 for (AttributeVecCIter ia = attributes.begin(); ia != attributes.end(); ia++) {
00184 *aml_output << " " << ia->getName() << " = \"" << to_xml_string(ia->getValue()) << "\"";
00185 }
00186
00187
00188
00189 bool generate_new_file =
00190 (aml_rep->getUniversalName() == "{http://www.w3.org/2001/XInclude}include" && !one_file);
00191
00192 if (children.size() == 0 || generate_new_file) {
00193 *aml_output << " />" << endl;
00194 } else {
00195 *aml_output << ">" << endl;
00196 }
00197
00198
00199
00200
00201 int child_indent = indent + 2;
00202 bool null_root = false;
00203 aml_out = aml_output;
00204
00205 if (generate_new_file) {
00206 string inc_name = aml_rep->getAttributeString("href");
00207 if (inc_name == "") {
00208 info_out.error ("No file name given for creating an AML file.");
00209 return false;
00210 }
00211 file_stack.addFile(inc_name);
00212 string f_name = file_stack.file_list.back().full_name;
00213 aml_out = new ofstream(f_name.c_str());
00214 if (!aml_out->is_open()) {
00215 info_out.error ("Cannot open file: " + f_name);
00216 return false;
00217 }
00218 if (children.size() > 1) {
00219 *aml_out << "<null_root>" << endl;
00220 null_root = true;
00221 }
00222 child_indent = 2;
00223 }
00224
00225
00226
00227 for (NodeVecCIter it = children.begin(); it != children.end(); it++) {
00228 if (!aml_rep_to_aml_file (*it, one_file, aml_out, file_stack, child_indent)) ok = false;
00229 }
00230
00231
00232
00233 if ((aml_rep->getUniversalName() != "{http://www.w3.org/2001/XInclude}include" || one_file) &&
00234 children.size() > 0) *aml_out << string(indent, ' ') << "</" << node_name << ">" << endl;
00235
00236 if (null_root) *aml_out << "</null_root>" << endl;
00237
00238
00239
00240 if (node_name == "{http://www.w3.org/2001/XInclude}include" && !one_file) {
00241 aml_out->close();
00242 delete aml_out;
00243 }
00244
00245 return ok;
00246
00247 }