]> err.no Git - sope/blob - sope-xml/samples/domxml.m
fixed copyrights for 2005
[sope] / sope-xml / samples / domxml.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include <SaxObjC/SaxObjC.h>
23 #include "common.h"
24 #include <DOM/DOMBuilderFactory.h>
25 #include <DOM/DOMXMLOutputter.h>
26 #include <DOM/DOMPYXOutputter.h>
27 #include <DOM/DOMSaxBuilder.h>
28
29 /*
30   Usage
31   
32     domxml [options] files
33       -repeat <n-times>
34       -xml|-pyx         - output in XML or PYX format ...
35 */
36
37 int main(int argc, char **argv, char **env) {
38   NSEnumerator      *paths;
39   NSString          *path;
40   NSAutoreleasePool *pool;
41   id<DOMBuilder>    builder;
42   id                out;
43   unsigned          repeat;
44   
45 #if LIB_FOUNDATION_LIBRARY
46   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
47 #endif
48
49   pool = [[NSAutoreleasePool alloc] init];
50
51   out    = nil;
52   repeat = 1;
53
54   builder = [[DOMBuilderFactory standardDOMBuilderFactory]
55                                 createDOMBuilder];
56   if (builder == nil) {
57     fprintf(stderr, "could not create DOM builder !\n");
58     exit(2);
59   }
60   
61   /* parse */
62
63   paths = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
64   [paths nextObject]; // skip toolname
65   while ((path = [paths nextObject])) {
66     NSAutoreleasePool *pool;
67     NSDate            *date;
68     NSTimeInterval    duration;
69     unsigned          i;
70     id doc;
71     
72     if ([path hasPrefix:@"-"]) {
73       if ([path isEqualToString:@"-pyx"]) {
74         out = [[[DOMPYXOutputter alloc] init] autorelease];
75       }
76       else if ([path isEqualToString:@"-xml"]) {
77         out = [[[DOMXMLOutputter alloc] init] autorelease];
78       }
79       else if ([path isEqualToString:@"-repeat"]) {
80         repeat = [[paths nextObject] intValue];
81       }
82       else {
83         // a default ? skip the value
84         [paths nextObject];
85       }
86       
87       continue;
88     }
89     
90     pool = [[NSAutoreleasePool alloc] init];
91
92     if (repeat > 1)
93       NSLog(@"repeat %i times ...", repeat);
94
95     doc = nil;
96     for (i = 0; i < repeat; i++) {
97       NSAutoreleasePool *pool2;
98       
99       pool2 = [[NSAutoreleasePool alloc] init];
100       [doc release]; doc = nil;
101       
102       date = [NSDate date];
103       doc = [(id)builder buildFromContentsOfFile:path];
104       duration = [[NSDate date] timeIntervalSinceDate:date];
105       if (doc)
106         NSLog(@"doc(%i) is %@, parsed in %.3fs", i, doc, duration);
107       else if (doc == nil) {
108         NSLog(@"couldn't build DOM from path '%@' (%.3fs)", path, duration);
109         [pool release]; pool = nil;
110         break;
111       }
112       
113       doc = [doc retain];
114       [pool2 release];
115     }
116     
117     //NSLog(@"doc is %@, parsed in %.3fs", doc, duration);
118     
119     if (doc == nil)
120       continue;
121     
122     [out outputDocument:doc to:nil];
123     
124     [pool release];
125   }
126   
127   [pool release];
128
129   exit(0);
130   return 0;
131 }