aboutsummaryrefslogtreecommitdiff
blob: 3b580f2e04546c63bc234ccd7687eeb3a2aef156 (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
/*
 * Copyright (C) 2004, Jan Brinkmann <lucky@the-luckyduck.de>
 * Copyright (c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
 * Copyright (c) 2004, Thomas Matthijs <axxo@gentoo.org>
 * Copyright (c) 2004, Gentoo Foundation
 * 
 * Licensed under the GNU General Public License, v2
 *
 */

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class JXSLT
{
    public static void printHelp()
    {
        System.err
                .println("Usage: java JXSLT ( -v <version> || -s <version> -t <version> ) -x <build.xslt> -i <oldbuild.xml> -o <newbuild.xml> ");
    }

    public static void main(String[] args)
    {
        // check if there are enough options given
        if (args.length <= 8)
        {
            System.err.println("missing options");
            printHelp();
            System.exit(1);
        }

        // detailed parsing of command line arguments
        File oldXmlFile = null, newXmlFile = null, xsltFile = null;
        String source = null, target = null;
        int i = 0;
        while (i < args.length)
        {
            boolean match = false;
            String[] options = {
                    "-v", "--version", "-s", "--source", "-t", "--target", "-x", "--xsltsource", "-i", "--oldxml", "-o", "--newxml"
            };
            
            if (args[i].substring(0, 1).equals("-"))
            {
                if (args[i+1].substring(0, 1).equals("-")) {
                    System.err.println("missing argument for '"+args[i]+"'");
                    printHelp();
                    System.exit(1);
                }
                
                int j = 0;
                while (j < options.length)
                {
                    if (options[j].equals(args[i]))
                    {
                        match = true;
                        break;
                    }
                    ++j;
                }

                if (match != true)
                {
                    System.err.println("invalid option '" + args[i] + "'");
                    printHelp();
                    System.exit(1);
                }
            } 

            if (args[i].equalsIgnoreCase("-v") || args[i].equalsIgnoreCase("--version"))
            {
                target = source = args[i + 1];
            } else if (args[i].equalsIgnoreCase("-s") || args[i].equalsIgnoreCase("--source")) 
			{
				source = args[i + 1];
			}  else if (args[i].equalsIgnoreCase("-t") || args[i].equalsIgnoreCase("--target")) 
			{
				target = args[i + 1];
			} else if (args[i].equalsIgnoreCase("-x")
                    || args[i].equalsIgnoreCase("--xsltsource"))
            {
                xsltFile = new File(args[i + 1]);
            } else if (args[i].equalsIgnoreCase("-i") || args[i].equalsIgnoreCase("--oldxml"))
            {
                oldXmlFile = new File(args[i + 1]);
            } else if (args[i].equalsIgnoreCase("-o") || args[i].equalsIgnoreCase("--newxml"))
            {
                newXmlFile = new File(args[i + 1]);
            }

            ++i;
        }
        
        // check if files exist
        Source xmlSource = null, xsltSource = null;
        if (oldXmlFile.exists())
        {
            xmlSource = new StreamSource(oldXmlFile);
        } else
        {
            System.out.println("xml sourcefile doesn't exist");
            System.exit(1);
        }

        if (xsltFile.exists())
        {
            xsltSource = new StreamSource(xsltFile);
        } else
        {
            System.out.println("xslt sourcefile doesn't exist");
            System.exit(1);
        }
        Result result = new StreamResult(newXmlFile);

        // create a new transformer and perform a transformation
        TransformerFactory transFact = TransformerFactory.newInstance();
        Transformer trans = null;
        try
        {
            trans = transFact.newTransformer(xsltSource);
            trans.setParameter("source", source);
            trans.setParameter("target", target);
            trans.transform(xmlSource, result);
            System.out.println(oldXmlFile + " transformed to " + newXmlFile);
			System.exit(0);
        } catch (TransformerConfigurationException e)
        {
            e.printStackTrace();
        } catch (TransformerException e)
        {
            e.printStackTrace();
        }
    }
}