kxslt1.c

最終更新:2009/12/19

kxslt1.c

001: /*
002:  * kxslt1.c
003:  */
004: 
005: #define USAGE "Usage: kxslt1 <in_file> <xsl_file> <out_file>"
006: 
007: #include "mystab.h"
008: #include "myio_apr.h"
009: #include "myio_xml.h"
010: 
011: int apr_my_main (
012:   int ac
013:   , char **av
014:   , apr_file_t * astdin
015:   , apr_file_t * astdout
016:   , apr_file_t * astderr
017:   , apr_pool_t * pool
018: ) {
019: 
020:   int error_flag = 0;
021:   
022:   char *in_filename = NULL;
023:   char *xsl_filename = NULL;
024:   char *out_filename = NULL;
025: 
026:   MYIO *in = NULL;
027:   MYIO *out = NULL;
028:   
029:   xmlDocPtr srcDom = NULL;
030:   xsltStylesheetPtr style = NULL;
031:   xmlDocPtr res = NULL;
032:     
033:   if (ac < 3) {
034:     apr_file_printf(astderr, "%s\n", USAGE);
035:     error_flag = 1;
036:     goto _FINALLY_;
037:   }
038:   
039:   in_filename = av[1];
040:   xsl_filename = av[2];
041:   out_filename = av[3];
042:   
043:   /* 入力ファイルのオープン */
044:   
045:   in = MYIO_fp_open(in_filename, APR_FOPEN_READ|APR_FOPEN_BINARY, APR_OS_DEFAULT, pool);
046:   if (! in) {
047:     apr_file_printf(astderr, "ERROR: MYIO_fp_open: %s ", in_filename);
048:     error_flag = 1;
049:     goto _FINALLY_;
050:   }
051:   
052:   out = MYIO_fp_open(out_filename, 
053:       APR_FOPEN_WRITE|APR_FOPEN_CREATE|APR_FOPEN_BINARY|APR_FOPEN_TRUNCATE,
054:         APR_OS_DEFAULT, pool);
055:   if (!out) {
056:     apr_file_printf(astderr, "ERROR: file open [%s] ", out_filename);
057:     error_flag = 1;
058:     goto _FINALLY_;
059:   }
060:   
061:   srcDom = my_xml_read(in);
062:   if (!srcDom) {
063:     apr_file_printf(astderr, "ERROR: my_xml_read [%s] ", in_filename);
064:     error_flag = 1;
065:     goto _FINALLY_;
066:   }
067:   
068:   style = xsltParseStylesheetFile ((const xmlChar*)xsl_filename);
069:   if (!style) {
070:     apr_file_printf(astderr, "ERROR: xsltParseStylesheetFile [%s] ", xsl_filename);
071:     error_flag = 1;
072:     goto _FINALLY_;
073:   }
074:   
075:   res = my_xml_transform(srcDom, style);
076:   if (!res) {
077:     apr_file_printf(astderr, "ERROR: my_xml_transform ");
078:     error_flag = 1;
079:     goto _FINALLY_;
080:   }
081:   
082:   {
083:     int wrote_size = MYIO_xslt_write(out, res, style);
084:     if (wrote_size<0) {
085:       error_flag = 1;
086:       apr_file_printf(astderr, "ERROR: MYIO_xslt_write ");
087:     } else {
088:       apr_file_printf(astderr, "%d bytes saved to %s.\n", wrote_size, out_filename);
089:     }
090:   }
091:   
092:   _FINALLY_:
093:   
094:   if (style) {
095:     xsltFreeStylesheet(style);
096:     style = NULL;
097:   }
098:   if (srcDom) {
099:     xmlFreeDoc(srcDom);
100:     srcDom = NULL;
101:   }
102:   if (res) {
103:     xmlFreeDoc(res);
104:     res = NULL;
105:   }
106:   xmlCleanupParser();
107:   xsltCleanupGlobals();
108:   xmlMemoryDump();
109:   
110:   if (in) {
111:     MYIO_fp_close(in);
112:     in = NULL;
113:   }
114:   
115:   if (out) {
116:     MYIO_fp_close(out);
117:     out = NULL;
118:   }
119:   
120:   if (error_flag) {
121:     apr_file_printf(astderr, "failed.\n");
122:     return 1; /* 異常終了 */
123:   }
124:   
125:   apr_file_printf(astdout, "done.\n");
126:   return 0; /* 正常終了 */
127:   
128: }
Copyright (C) KAKU PROJECT (2009)KAKU PROJECT (2009)