00001 #include <stdio.h>
00002 #include "vapiTypes.h"
00003 #include "vapiAux.h"
00004 #include "vapiString.h"
00005 #include "vapiMacro.h"
00006 #include "vapiMacroList.h"
00007 #include "vapiOperationList.h"
00008 #include "vapiOperationOptions.h"
00009 #include "vapiGtkAux.h"
00010 #include "vapiGtkAuxWindow.h"
00011 #include "vapiOptions.h"
00012 #include "vapiOptionsXML.h"
00013 #include "vapiXML.h"
00014 #include "vapiOperationXML.h"
00015 #include <libxml/encoding.h>
00016 #include <libxml/xmlreader.h>
00017 #include <libxml/xmlwriter.h>
00018 #include <string.h>
00019
00020 static xmlTextWriter *
00021 vapiMacroXMLFileInit (vMacroHeader * MacroHeader, const char *filename)
00022 {
00023 xmlTextWriter *xmlOutput;
00024
00025
00026
00027
00028
00029 xmlOutput = xmlNewTextWriterFilename (filename, 0);
00030
00031 xmlTextWriterSetIndent (xmlOutput, 1);
00032
00033 xmlTextWriterStartDocument (xmlOutput, NULL, "ISO-8859-1", NULL);
00034
00035 xmlTextWriterStartDTD (xmlOutput, BAD_CAST "vapiMacro", NULL, NULL);
00036
00037 xmlTextWriterWriteDTDElement (xmlOutput,
00038 BAD_CAST "vapiMacro",
00039 BAD_CAST "(Operation*,Comment?)");
00040 xmlTextWriterWriteDTDAttlist (xmlOutput,
00041 BAD_CAST "vapiMacro",
00042 BAD_CAST "name NMTOKENS #REQUIRED");
00043 xmlTextWriterWriteDTDElement (xmlOutput, BAD_CAST "Comment",
00044 BAD_CAST "(#PCDATA)");
00045 xmlTextWriterWriteDTDElement (xmlOutput,
00046 BAD_CAST "Operation",
00047 BAD_CAST "(option)*");
00048 xmlTextWriterWriteDTDAttlist (xmlOutput,
00049 BAD_CAST "Operation",
00050 BAD_CAST "name NMTOKENS #REQUIRED");
00051
00052 vapiOptionsXMLOptionsElement (xmlOutput, 2, "times", "original");
00053
00054 xmlTextWriterEndDTD (xmlOutput);
00055
00056 xmlTextWriterStartElement (xmlOutput, BAD_CAST "vapiMacro");
00057 xmlTextWriterWriteAttribute (xmlOutput, BAD_CAST "name",
00058 BAD_CAST MacroHeader->MacroName);
00059
00060 xmlTextWriterStartElement (xmlOutput, BAD_CAST "Comment");
00061 xmlTextWriterWriteString (xmlOutput, BAD_CAST MacroHeader->MacroComment);
00062 xmlTextWriterEndElement (xmlOutput);
00063
00064 return xmlOutput;
00065 }
00066
00067 void
00068 vapiMacroToXML (vMacroHeader * MacroHeader, const char *filename)
00069 {
00070 xmlTextWriter *xmlOutput;
00071 int i, OperationNumber;
00072 vMacroItem *Item;
00073
00074
00075
00076
00077
00078 xmlOutput = vapiMacroXMLFileInit (MacroHeader, filename);
00079
00080
00081
00082
00083
00084 OperationNumber = vapiMacroCountElements (MacroHeader->Macro);
00085
00086 if (!OperationNumber)
00087 {
00088 return;
00089 }
00090
00091 for (i = 0; i < OperationNumber; i++)
00092 {
00093 Item = vapiMacroSelectItem (MacroHeader->Macro, i);
00094
00095 xmlTextWriterStartElement (xmlOutput, BAD_CAST "Operation");
00096
00097 xmlTextWriterWriteAttribute (xmlOutput, BAD_CAST "name",
00098 BAD_CAST Item->Operation->name);
00099
00100
00101
00102
00103 vapiOptionsXMLGetOptions (xmlOutput,
00104 Item->Operation->OptionsSettings,
00105 Item->Options,
00106 &vapiOptionsXMLGetOptionsOperation);
00107
00108
00109 xmlTextWriterEndElement (xmlOutput);
00110
00111 }
00112
00113
00114 xmlTextWriterEndDocument (xmlOutput);
00115
00116 xmlFreeTextWriter (xmlOutput);
00117 v_info ("Guardada Macro para xml");
00118
00119 }
00120
00121 vMacroHeader *
00122 vapiMacroFromXML (const char *filename, vOperationList * OperationList,
00123 vMacroList * MacroList)
00124 {
00125 xmlTextReader *xmlInput;
00126 vMacroHeader *MacroHeader;
00127 vEffect *tempOperation;
00128 vOptions *tempOptions;
00129 int ret = 1;
00130 xmlInput = xmlReaderForFile (filename, "ISO-8859-1", XML_PARSE_DTDATTR |
00131 XML_PARSE_NOENT |
00132 XML_PARSE_DTDVALID);
00133
00134 if (xmlInput == NULL)
00135 {
00136 return NULL;
00137 }
00138 xmlTextReaderRead (xmlInput);
00139 if (!strcmp
00140 ((char *) xmlTextReaderLocalName (xmlInput), "vapiMacro "))
00141 {
00142 return MacroHeader = 0;
00143 }
00144
00145 vapiXMLNextElement (xmlInput);
00146 MacroHeader =
00147 vapiMacroNew ((char *) xmlTextReaderGetAttribute
00148 (xmlInput, BAD_CAST "name"));
00149
00150
00151
00152 if (vapiXMLNextElement (xmlInput) > 0)
00153 {
00154 if (!strcmp
00155 ((char *) xmlTextReaderLocalName (xmlInput), "Comment"))
00156 {
00157 v_debug ("comentário lido: .%s.", xmlTextReaderReadString
00158 (xmlInput));
00159 vapiMacroUpdateComment (MacroHeader,
00160 (char *)
00161 xmlTextReaderReadString
00162 (xmlInput));
00163 v_debug ("comentário lido: .%s.", xmlTextReaderReadString
00164 (xmlInput));
00165 vapiXMLNextElement (xmlInput);
00166
00167 }
00168 if (strcmp
00169 ((char *) xmlTextReaderLocalName (xmlInput), "Operation"))
00170 {
00171 return MacroHeader = 0;
00172 }
00173
00174 while (ret > 0)
00175 {
00176
00177 tempOperation =
00178 vapiOperationListSelectOperation
00179 (OperationList, (char *)
00180 xmlTextReaderGetAttribute (xmlInput,
00181 BAD_CAST "name"));
00182 v_debug ("operation name: %s",
00183 xmlTextReaderGetAttribute (xmlInput,
00184 BAD_CAST "name"));
00185
00186 tempOptions =
00187 vapiOperationOptionsInit (tempOperation);
00188
00189 vapiOptionsXMLSetOptions (xmlInput, tempOptions,
00190 &vapiOptionsXMLSetOptionsOperation);
00191
00192 vapiMacroAddItem (MacroHeader->Macro,
00193 tempOperation, tempOptions);
00194 ret = vapiXMLNextElement (xmlInput);
00195
00196 vapiOptionsCheck (tempOperation->OptionsSettings,
00197 tempOptions);
00198
00199 vapiCheckOperations (tempOperation, tempOptions,
00200 MacroList);
00201 }
00202
00203 }
00204
00205
00206
00207 return MacroHeader;
00208 }
00209
00210 char *
00211 vapiMacroFromXMLSetName (const char *filename)
00212 {
00213
00214 xmlTextReader *xmlInput;
00215
00216 xmlInput = xmlReaderForFile (filename, "ISO-8859-1", XML_PARSE_DTDATTR |
00217 XML_PARSE_NOENT |
00218 XML_PARSE_DTDVALID);
00219
00220 if (xmlInput == NULL)
00221 {
00222 return NULL;
00223 }
00224 if (xmlTextReaderRead (xmlInput) < 0)
00225 {
00226 return NULL;
00227 }
00228
00229 if (!strcmp
00230 ((char *) xmlTextReaderLocalName (xmlInput), "vapiMacro "))
00231 {
00232 return NULL;
00233 }
00234
00235 vapiXMLNextElement (xmlInput);
00236 return ((char *)
00237 xmlTextReaderGetAttribute (xmlInput, BAD_CAST "name"));
00238 }
00239
00240 vBoolean
00241 vapiMacroXMLLoadToList (vMacroList * MacroList,
00242 vOperationList * OperationList, const char *filename)
00243 {
00244 char *MacroName;
00245 vMacroHeader *MacroHeader;
00246
00247 MacroName = vapiMacroFromXMLSetName (filename);
00248
00249 if (MacroName == NULL)
00250 {
00251 return vFALSE;
00252 }
00253
00254 if (vapiMacroListMacroExist (MacroList, MacroName, 1) == -1)
00255 {
00256 return vFALSE;
00257 }
00258
00259 MacroHeader = vapiMacroFromXML (filename, OperationList, MacroList);
00260
00261 vapiMacroListAddItem (MacroList, MacroHeader);
00262
00263 return vTRUE;
00264
00265 }