00001 #include "system.h" 00002 const char *__progname; 00003 00004 #include <header.h> 00005 #define _RPMXP_INTERNAL 00006 #include "rpmxp.h" 00007 #include "debug.h" 00008 00009 int _rpmxp_debug = 0; 00010 00011 const char * rpmxpDTD = "\ 00012 <?xml version=\"1.0\"?>\n\ 00013 <!DOCTYPE rpmHeader [\n\ 00014 <!ELEMENT rpmHeader (rpmTag+)>\n\ 00015 <!ELEMENT rpmTag (string+|integer+|base64+)>\n\ 00016 <!ATTLIST rpmTag name CDATA #REQUIRED>\n\ 00017 <!ELEMENT string (#PCDATA)>\n\ 00018 <!ELEMENT integer (#PCDATA)>\n\ 00019 <!ELEMENT base64 (#PCDATA)>\n\ 00020 ]>\n\ 00021 "; 00022 00023 rpmxp rpmxpFree(rpmxp xp) 00024 { 00025 if (xp != NULL) { 00026 if (xp->value) { 00027 xmlFree(xp->value); 00028 xp->value = NULL; 00029 } 00030 if (xp->name) { 00031 xmlFree(xp->name); 00032 xp->name = NULL; 00033 } 00034 if (xp->reader != NULL) { 00035 xmlFreeTextReader(xp->reader); 00036 xp->reader = NULL; 00037 } 00038 free(xp); 00039 xp = NULL; 00040 } 00041 return xp; 00042 } 00043 00044 rpmxp rpmxpNew(const char * fn) 00045 { 00046 rpmxp xp = calloc(1, sizeof(*xp)); 00047 00048 if (xp == NULL) 00049 return NULL; 00050 if (fn) { 00051 int xx; 00052 xp->reader = xmlNewTextReaderFilename(fn); 00053 if (xp->reader == NULL) 00054 return rpmxpFree(xp); 00055 xx = xmlTextReaderSetParserProp(xp->reader, XML_PARSER_VALIDATE, 1); 00056 xx = xmlTextReaderSetParserProp(xp->reader, XML_PARSER_SUBST_ENTITIES, 1); 00057 } 00058 xp->name = NULL; 00059 xp->value = NULL; 00060 xp->depth = 0; 00061 xp->nodeType = 0; 00062 xp->isEmptyElement = 0; 00063 xp->n = 2; 00064 return xp; 00065 } 00066 00067 int rpmxpRead(rpmxp xp) 00068 { 00069 return xmlTextReaderRead(xp->reader); 00070 } 00071 00072 int rpmxpProcess(rpmxp xp) 00073 { 00074 int rc = 0; 00075 00076 xp->name = xmlTextReaderName(xp->reader); 00077 xp->value = xmlTextReaderValue(xp->reader); 00078 xp->depth = xmlTextReaderDepth(xp->reader); 00079 xp->nodeType = xmlTextReaderNodeType(xp->reader); 00080 xp->isEmptyElement = xmlTextReaderIsEmptyElement(xp->reader); 00081 00082 if (xp->name == NULL) 00083 xp->name = xmlStrdup(BAD_CAST "--"); 00084 00085 if (_rpmxp_debug) 00086 printf("%d %d %s %d\n", xp->depth, xp->nodeType, xp->name, xp->isEmptyElement); 00087 switch (xp->nodeType) { 00088 case XML_READER_TYPE_ELEMENT: 00089 printf("%*s<%s", (xp->n * xp->depth), "", xp->name); 00090 while (xmlTextReaderMoveToNextAttribute(xp->reader) != 0) { 00091 xmlChar * attrN = xmlTextReaderName(xp->reader); 00092 xmlChar * attrV = xmlTextReaderValue(xp->reader); 00093 printf(" %s", attrN); 00094 if (attrV) { 00095 printf("=\"%s\"", attrV); 00096 xmlFree(attrV); 00097 } 00098 xmlFree(attrN); 00099 } 00100 if (xp->isEmptyElement) 00101 printf("/>\n"); 00102 else { 00103 printf(">"); 00104 if (xp->depth < 2) 00105 printf("\n"); 00106 } 00107 break; 00108 case XML_READER_TYPE_END_ELEMENT: 00109 if (xp->depth < 2) 00110 printf("%*s", (xp->n * xp->depth), ""); 00111 printf("</%s>\n", xp->name); 00112 break; 00113 case XML_READER_TYPE_TEXT: 00114 printf("%s", xp->value); 00115 break; 00116 case XML_READER_TYPE_DOCUMENT_TYPE: 00117 break; 00118 case XML_READER_TYPE_SIGNIFICANT_WHITESPACE: 00119 break; 00120 00121 case XML_READER_TYPE_NONE: 00122 case XML_READER_TYPE_ATTRIBUTE: 00123 case XML_READER_TYPE_CDATA: 00124 case XML_READER_TYPE_ENTITY_REFERENCE: 00125 case XML_READER_TYPE_ENTITY: 00126 case XML_READER_TYPE_PROCESSING_INSTRUCTION: 00127 case XML_READER_TYPE_COMMENT: 00128 case XML_READER_TYPE_DOCUMENT: 00129 case XML_READER_TYPE_DOCUMENT_FRAGMENT: 00130 case XML_READER_TYPE_NOTATION: 00131 case XML_READER_TYPE_WHITESPACE: 00132 case XML_READER_TYPE_END_ENTITY: 00133 case XML_READER_TYPE_XML_DECLARATION: 00134 default: 00135 printf("%d %d %s %d\n", xp->depth, xp->nodeType, 00136 xp->name, xp->isEmptyElement); 00137 if (xp->value) 00138 printf(" %s", xp->value); 00139 if (xp->depth < 2) 00140 printf("\n"); 00141 rc = -1; 00142 break; 00143 } 00144 00145 00146 if (xp->value != NULL) { 00147 xmlFree(xp->value); 00148 xp->value = NULL; 00149 } 00150 if (xp->name != NULL) { 00151 xmlFree(xp->name); 00152 xp->name = NULL; 00153 } 00154 return rc; 00155 } 00156 00157 int rpmxpParseFile(rpmxp xp) 00158 { 00159 int ret = -1; 00160 00161 if (xp != NULL) 00162 while ((ret = rpmxpRead(xp)) == 1) 00163 rpmxpProcess(xp); 00164 return ret; 00165 }