summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/wcps/server/cli/xml.java
blob: 8a262ca77d7af7556f15c89c8acb2e91d13709c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003 - 2010 Peter Baumann / rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 */
package petascope.wcps.server.cli;

import petascope.core.DbMetadataSource;
import petascope.wcps.server.core.ProcessCoveragesRequest;
import petascope.wcps.server.core.WCPS;
import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import java.util.Properties;
import org.xml.sax.InputSource;

/**
 * This is a small application around the WCPS core. It takes XML requests as files and runs them
 * through WCPS. The resulting radaman queries are displayed, but not executed. This is very useful
 * for testing whether your metadata is valid.
 */
public class xml {

    private static WCPS wcps;
    private static DbMetadataSource metadataSource;

    private static void initMetadata() {
        File cwd = new File(".");
        System.out.println("Working in " + cwd.getAbsolutePath());
        String pcSchemaFileName =
                //		"src/conf/" +
                "xml" + File.separator + "ogc" + File.separator + "wcps"
                + File.separator + "1.0.0" + File.separator + "wcpsProcessCoverages.xsd";
        File pcSchemaFile = new File(pcSchemaFileName);

        if (!pcSchemaFile.exists()) {
            System.err.println("WCPS: could not find the WCPS ProcessCoverage schema ("
                    + pcSchemaFileName + ")");
            System.exit(1);
        }

        metadataSource = null;

        try {
            Properties dbParams = new Properties();

            dbParams.load(new FileInputStream("settings.properties"));
            metadataSource =
                    new DbMetadataSource(dbParams.getProperty("metadata_driver"),
                    dbParams.getProperty("metadata_url"),
                    dbParams.getProperty("metadata_user"),
                    dbParams.getProperty("metadata_pass"), false);
            wcps = new WCPS(pcSchemaFile, metadataSource);
        } catch (Exception e) {
            System.err.println("WCPS: could not initialize WCPS:");
            e.printStackTrace(System.err);
            System.exit(1);
        }
    }

    public static void main(String[] args) {
        if (args.length < 1) {
            System.err.println("WCPS CLI: no input files");

            System.err.println("\nWCPS CLI Usage: java wcps.server.cli.xml input.xml");
            System.err.println("Where input.xml contains a ProcessCoverages Request ");
//            System.exit(1);

            args = new String[1];
            args[0] = "test/testcases-wcps_dollar/25.test.xml";
        }
        if (args.length > 1) {
            System.err.println("WCPS: no input files");
            System.exit(1);
        }

        initMetadata();

        for (int i = 0; i < args.length; i++) {
            File fileIn = null;
            InputSource is = null;

            try {
                fileIn = new File(args[i]);
                is = new InputSource(new FileInputStream(fileIn));
            } catch (Exception fnfe) {
                System.err.println("WCPS: " + args[i]
                        + ": no such file or directory" + fnfe);
                System.exit(1);
            }


            String result = processCoverage(is, i);
            if (result != null) {
                System.out.println(result);
            } else {
                System.err.println("WCPS: " + args[i] + " failed");
                System.exit(1);
            }
        }

        metadataSource.close();
        System.exit(0);

    }

    private static String processCoverage(InputSource is, int i) {
        String result = null;

        try {
            ProcessCoveragesRequest r = wcps.pcPrepare("http://kahlua.eecs.jacobs-university.de:9001",
                    "RASSERVICE", is);
            System.err.println("Request " + i);
            String rasql = r.getRasqlQuery();
            String mime = r.getMime();
//			result = "[" + mime + "] " + rasql;
            result = rasql;
        } catch (Exception e) {
            System.err.println("WCPS: request " + i
                    + " failed with the following exception:");
            e.printStackTrace(System.err);
        }

        return result;
    }

    /** Converts a WCPS XML query into a RasQL query string **/
    public static String convertXmlToRasql(String query) {
        String rasql = null;
        if (metadataSource == null) {
            initMetadata();
        }
        InputSource is = new InputSource(new StringReader(query));
        rasql = processCoverage(is, 1);
        return rasql;
    }
}