source: main/trunk/greenstone2/runtime-src/src/recpt/formatconverter.cpp@ 29042

Last change on this file since 29042 was 29042, checked in by ak19, 10 years ago

First of 2 part commit for improving FormatConversion from GS2 to GS3. formatconverter.exe now takes an additional optional parameter which can be documentNode or classifierNode. This then determines what the formatconverter.exe does when it sees an If test on the existence of the numleafdocs variable, since a positive test applies only to classifierNodes, while a negative test applies only to documentNodes. Further, [link][icon][link] should output something slightly different for classifierNodes than for documentNodes.

File size: 3.7 KB
Line 
1/**********************************************************************
2 *
3 * formatconverter.cpp --
4 * Copyright (C) 2013 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "formattools.h"
27
28#include<string> // string
29#include <cstring> // char *, strlen, strcpy
30#include<iostream>
31using namespace std;
32
33/*********************************************************************
34Takes a GS2 format statement as input, which can be multi-line. It prints out a GS3 format statement.
35
36E.g. {If}{[ex.FileFormat] eq 'PDF',{Or}{[ex.thumbicon],[ex.srcicon]},[ex.srclink]{Or}{[ex.thumbicon],[ex.srcicon]}[ex./srclink]}
37
38should output:
39<gsf:switch><gsf:metadata name="[ex.FileFormat]"/><gsf:when test="equals" test-value="PDF"><gsf:choose-metadata><gsf:metadata name="thumbicon" /><gsf:metadata name="srcicon" /></gsf:choose-metadata></gsf:when><gsf:otherwise><gsf:link type='source'></gsf:otherwise></gsf:switch>
40
41Hitting Enter inserts a newline. Use Ctrl-Z (Win), Ctrl-D (Lin/Mac) to terminate input.
42
43To run the same from a script and do so silently, so that the only output is the result string:
44> echo "I
45was
46here" | ./formatconverter --silent
47***********************************************************************/
48void print_usage() {
49 cout << endl << "Usage: formatconverter [options]" << endl;
50 cout << "\tOptions: --help, [--silent] [--documentNode|--classifierNode]" << endl << endl;
51}
52
53int main(int argc, char **argv) {
54 text_t argstr;
55 text_tset metadata;
56 bool getParents;
57 format_t *formatlistptr = new format_t();
58 text_t resultstring;
59
60 text_t nodeType = "";
61 bool silent = false;
62
63 if(argc > 1) {
64
65 if((strcmp(argv[1], "--help") == 0)) {
66 print_usage();
67 return 0;
68 }
69
70 else {
71 for(int i = 1; i < argc; i++) {
72 if (strcmp(argv[i], "--silent") == 0) { silent = true; }
73 else if(strcmp(argv[i], "--documentNode") == 0) { nodeType = "document"; }
74 else if(strcmp(argv[i], "--classifierNode") == 0) { nodeType = "classifier"; }
75 else {
76 print_usage();
77 return 0;
78 }
79 }
80 }
81 }
82
83 if(!silent) {
84 cout << "Enter EOF (Windows: Ctrl-Z | Linux: Ctrl-D | Mac OS X: Ctrl-D) to terminate input>\n";
85 }
86
87 // better to read in a line at a time
88 // http://stackoverflow.com/questions/13148887/yet-another-c-cin-loop-issue
89 // terminating input: http://www.cplusplus.com/forum/beginner/49993/
90
91 for (string line; getline(cin, line); )
92 {
93 argstr.appendcstr(line.c_str());
94 argstr += "\n";
95 }
96
97 parse_formatstring (argstr, formatlistptr, metadata, getParents);
98 resultstring = get_GS3_formatstring (formatlistptr, nodeType);
99
100 delete formatlistptr;
101
102 if(silent) {
103 cout << resultstring << endl;
104 } else {
105 cout << "Input: " << argstr << endl;
106 cout << "Transform result: " << resultstring << endl;
107 cout << "Done\n";
108 }
109
110 return 0;
111}
Note: See TracBrowser for help on using the repository browser.