summaryrefslogtreecommitdiffstats
path: root/src/java/net/sf/antcontrib/walls/WallsFileHandler.java
blob: f9e8f65db8d6d2fc5810890995e5e10e4f79123f (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
/*
 * Copyright (c) 2001-2004 Ant-Contrib project.  All rights reserved.
 *
 * 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 net.sf.antcontrib.walls;

import java.io.File;

import org.apache.tools.ant.Project;
import org.xml.sax.*;


/**
 * Handler for the root element. Its only child must be the "project" element.
 */
class WallsFileHandler extends HandlerBase {

    private final CompileWithWalls compilewithwalls;
    private File file = null;
    private Walls walls = null;
    private Locator locator = null;

    /**
     * @param CompileWithWalls
     */
    WallsFileHandler(CompileWithWalls walls, File file) {
        this.compilewithwalls = walls;
        this.file = file;
    }
        
    /**
     * Resolves file: URIs relative to the build file.
     *
     * @param publicId The public identifer, or <code>null</code>
     *                 if none is available. Ignored in this
     *                 implementation.
     * @param systemId The system identifier provided in the XML
     *                 document. Will not be <code>null</code>.
     */
    public InputSource resolveEntity(String publicId,
                                     String systemId) {
         compilewithwalls.log("publicId="+publicId+" systemId="+systemId,
             Project.MSG_VERBOSE);            
        return null;
    }

    /**
     * Handles the start of a project element. A project handler is created
     * and initialised with the element name and attributes.
     *
     * @param tag The name of the element being started.
     *            Will not be <code>null</code>.
     * @param attrs Attributes of the element being started.
     *              Will not be <code>null</code>.
     *
     * @exception SAXParseException if the tag given is not
     *                              <code>"project"</code>
     */
    public void startElement(String name, AttributeList attrs) throws SAXParseException {
        if (name.equals("walls")) {
            if(attrs.getLength() > 0)
                throw new SAXParseException("Error in file="+file.getAbsolutePath()
                                        +", no attributes allowed for walls element", locator);
            walls = this.compilewithwalls.createWalls();
        } else if (name.equals("package")) {
            handlePackage(attrs);
        } else {
            throw new SAXParseException("Error in file="+file.getAbsolutePath()
                                +", Unexpected element \"" + name + "\"", locator);
        }
    }

    private void handlePackage(AttributeList attrs) throws SAXParseException {
        if(walls == null)
            throw new SAXParseException("Error in file="+file.getAbsolutePath()
                                +", package element must be nested in a walls element", locator);
        
        String name = attrs.getValue("name");
        String thePackage = attrs.getValue("package");
        String depends = attrs.getValue("depends");
        if(name == null)
            throw new SAXParseException("Error in file="+file.getAbsolutePath()
                            +", package element must contain the 'name' attribute", locator);
        else if(thePackage == null)
            throw new SAXParseException("Error in file="+file.getAbsolutePath()
                            +", package element must contain the 'package' attribute", locator);

        Package p = new Package();
        p.setName(name);
        p.setPackage(thePackage);
        if(depends != null)
            p.setDepends(depends);
        
        walls.addConfiguredPackage(p);
    }
    /**
     * Sets the locator in the project helper for future reference.
     *
     * @param locator The locator used by the parser.
     *                Will not be <code>null</code>.
     */
    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }
}