summaryrefslogtreecommitdiffstats
path: root/tags/cpp-stable-20060304/sca/tools/scagen/src/org/apache/tuscany/sca/cpp/tools/services/XMLFileActor.java
blob: 047e1ab3f1dc8933cddcaebf09af71d43479cb5a (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.sca.cpp.tools.services;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;

import org.apache.tuscany.sca.cpp.tools.common.FileActor;
import org.apache.tuscany.sca.cpp.tools.common.Utils;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * The purpose of this abstract class is to provide a home for the standard
 * processing that is involved in turning a XML file into an internal DOM.
 */
public abstract class XMLFileActor implements FileActor {

    protected static Map handlers = new HashMap();

    protected static Map parameters = new HashMap();

    public boolean failed;
    
    private int filesActedOn=0;

    protected static TransformerFactory transformerFactory = TransformerFactory
            .newInstance();

    /**
     * This method is the main FileActor method
     * 
     * @see FileActor#actOnFile(File, File, int) Here we create an initial DOM
     *      and kick off the processing (using the handler map that has been set
     *      up by the concrete subclass).
     * 
     * @param moduleXML
     *            the sca.module or fragment file
     * @param target
     *            the target directory
     * @param depth
     *            not uesed here but in the
     * @see FileActor#actOnFile(File, File, int) interface to allow for
     *      recursive diving into a directory structure.
     */
    public void actOnFile(File moduleXML, File target, int depth)
            throws Exception {

        if (null == moduleXML || null == target) {
            return;
        }

        filesActedOn++;
        
        parameters.put("sourceFile", moduleXML);
        parameters.put("targetFile", target);

        if (transformerFactory.getFeature(DOMSource.FEATURE)
                && transformerFactory.getFeature(DOMResult.FEATURE)) {
            Document dom = createDomFromXMLFile(moduleXML);
            if (dom != null) {
                parameters.put("targetDirectoryFile", target);
                DomHandler.handleDom(dom, handlers, parameters);
            }
        }
    }

    /**
     * This method builds an in memory DOM from an XML file
     * 
     * @param xmlSourceFile
     *            the XML file we are handling
     * @return the resulting document
     */
    protected Document createDomFromXMLFile(File xmlSourceFile) {
        Document dom = null;
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
        //We do not validate via f.setValidating(true);
        f.setNamespaceAware(true);

        try {
            DocumentBuilder parser = f.newDocumentBuilder();
            dom = parser.parse(xmlSourceFile);
        } catch (SAXException sxe) {
            String path;
            try {
                path = xmlSourceFile.getCanonicalPath();
            } catch (IOException e) {
                path = xmlSourceFile.getPath();
            }
            Utils.screenMessage("There has been a SAXException of type "
                    + sxe.getLocalizedMessage());
            if (null != xmlSourceFile) {
                Utils.screenMessage(" when processing file " + path);
            } else {
                Utils.screenMessage(" as the input file is null.");
            }

// Leave for possible future debug option
//            Utils.screenMessage(" The returned Java exception is below.");
//            if (sxe.getException() != null)
//                sxe.getException().printStackTrace();
//            else
//                sxe.printStackTrace();
        } catch (ParserConfigurationException pce) {
            String path;
            try {
                path = xmlSourceFile.getCanonicalPath();
            } catch (IOException e) {
                path = xmlSourceFile.getPath();
            }
            Utils
                    .screenMessage("There has been a ParserConfigurationException of type "
                            + pce.getLocalizedMessage());
            if (null != xmlSourceFile) {
                Utils.screenMessage(" when processing file " + path);
            } else {
                Utils.screenMessage(" as the input file is null.");
            }
            
//          Leave for possible future debug option
//          Utils.screenMessage(" The returned Java exception is below.");
//          pce.printStackTrace();
            
        } catch (IOException ioe) {
            String path;
            try {
                path = xmlSourceFile.getCanonicalPath();
            } catch (IOException e) {
                path = xmlSourceFile.getPath();
            }
            
            Utils.screenMessage("Unable to open file  " + path);
            Utils.screenMessage(this.getContextMessage());
            
            
            
        }

        return dom;
    }

    /**
     * @return an error message - usually over-ridden.
     */
    private String getContextMessage() {
        return "Check the file exists and can be read.";
    }

    /**
     * Set a parameter
     * 
     * @param name
     * @param value
     */
    public void setParameter(String name, Object value) {
        parameters.put(name, value);
    }

    /**
     * Get a parameter
     * 
     * @param name
     * @param value
     * @return the value of the parameter
     */
    public Object getParameter(String name, Object value) {
        return parameters.get(name);
    }



    /**
     * @return Returns the filesActedOn.
     */
    public int getFilesActedOn() {
        return filesActedOn;
    }
}