~ubuntu-branches/ubuntu/oneiric/mkgmap/oneiric

« back to all changes in this revision

Viewing changes to src/uk/me/parabola/mkgmap/osmstyle/actions/ActionReader.java

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine, Andreas Putzo, Francesco Paolo Lovergine
  • Date: 2009-07-16 11:10:16 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090716111016-yycxqya1f26xmti7
Tags: 0.0.0+svn1067-1
[ Andreas Putzo ]
* New upstream snapshot.
* Added ${misc:Depends} among dependencies to fix a lintian warning.
* Bumped debhelper compatibility level to 7.
* Updated long description.
* Updated Homepage in debian/control, debian/copyright, debian/watch.
* Added numerous files from /doc to debian/docs.
* Mentioned Bernhard Heibler in debian/copyright and updated copyright
  year of software and packaging.
* Bumped policy to 3.8.2, without changes.
* Added DM-Upload-Allowed to debian/control.

[ Francesco Paolo Lovergine ]
* Added me as Uploader to avoid possible inappropriate NMU notices.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Steve Ratcliffe
 
3
 * 
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License version 2 as
 
6
 *  published by the Free Software Foundation.
 
7
 * 
 
8
 *  This program is distributed in the hope that it will be useful,
 
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *  GNU General Public License for more details.
 
12
 * 
 
13
 * 
 
14
 * Author: Steve Ratcliffe
 
15
 * Create date: 16-Nov-2008
 
16
 */
 
17
package uk.me.parabola.mkgmap.osmstyle.actions;
 
18
 
 
19
import java.util.ArrayList;
 
20
import java.util.List;
 
21
 
 
22
import uk.me.parabola.mkgmap.osmstyle.eval.SyntaxException;
 
23
import uk.me.parabola.mkgmap.scan.Token;
 
24
import uk.me.parabola.mkgmap.scan.TokenScanner;
 
25
 
 
26
/**
 
27
 * Read an action block.  This is contained within braces and contains
 
28
 * commands to change tags etc.
 
29
 * 
 
30
 * @author Steve Ratcliffe
 
31
 */
 
32
public class ActionReader {
 
33
        private final TokenScanner scanner;
 
34
 
 
35
        public ActionReader(TokenScanner scanner) {
 
36
                this.scanner = scanner;
 
37
        }
 
38
 
 
39
        public List<Action> readActions() {
 
40
                List<Action> actions = new ArrayList<Action>();
 
41
                scanner.skipSpace();
 
42
                if (!scanner.checkToken("{"))
 
43
                        return actions;
 
44
 
 
45
                scanner.nextToken();
 
46
                
 
47
                while (inAction()) {
 
48
                        Token tok = scanner.nextToken();
 
49
                        if (tok.isValue(";"))
 
50
                                continue;
 
51
 
 
52
                        String cmd = tok.getValue();
 
53
                        if ("set".equals(cmd)) {
 
54
                                actions.add(readTagValue(true));
 
55
                        } else if ("add".equals(cmd)) {
 
56
                                actions.add(readTagValue(false));
 
57
                        } else if ("apply".equals(cmd)) {
 
58
                                actions.add(readAllCmd());
 
59
                        } else if ("name".equals(cmd)) {
 
60
                                actions.add(readNameCmd());
 
61
                        } else if ("delete".equals(cmd)) {
 
62
                                String tag = scanner.nextWord();
 
63
                                actions.add(new DeleteAction(tag));
 
64
                        } else if ("rename".equals(cmd)) {
 
65
                                String from = scanner.nextWord();
 
66
                                String to = scanner.nextWord();
 
67
                                Action act = new RenameAction(from, to);
 
68
                                actions.add(act);
 
69
                        } else {
 
70
                                throw new SyntaxException(scanner, "Unrecognised command '" + cmd + '\'');
 
71
                        }
 
72
 
 
73
                        scanner.skipSpace();
 
74
                }
 
75
                if (scanner.checkToken("}"))
 
76
                        scanner.nextToken();
 
77
                scanner.skipSpace();
 
78
                return actions;
 
79
        }
 
80
 
 
81
        private Action readAllCmd() {
 
82
                String role = null;
 
83
                if (scanner.checkToken("role")) {
 
84
                        scanner.nextToken();
 
85
                        String eq = scanner.nextValue();
 
86
                        if (!"=".equals(eq))
 
87
                                throw new SyntaxException(scanner, "Expecting '=' after role keyword");
 
88
                        role = scanner.nextWord();
 
89
                }
 
90
                SubAction subAction = new SubAction(role);
 
91
 
 
92
                List<Action> actionList = readActions();
 
93
                for (Action a : actionList)
 
94
                        subAction.add(a);
 
95
                
 
96
                return subAction;
 
97
        }
 
98
 
 
99
        /**
 
100
         * A name command has a number of alternatives separated by '|' characters.
 
101
         */
 
102
        private Action readNameCmd() {
 
103
                NameAction nameAct = new NameAction();
 
104
                while (inActionCmd()) {
 
105
                        if (scanner.checkToken("|")) {
 
106
                                scanner.nextToken();
 
107
                                continue;
 
108
                        }
 
109
                        String val = scanner.nextWord();
 
110
                        nameAct.add(val);
 
111
                }
 
112
                return nameAct;
 
113
        }
 
114
 
 
115
        private AddTagAction readTagValue(boolean modify) {
 
116
                String key = scanner.nextWord();
 
117
                if (!scanner.checkToken("="))
 
118
                        throw new SyntaxException(scanner, "Expecting tag=value");
 
119
                scanner.nextToken();
 
120
                String val = scanner.nextWord();
 
121
 
 
122
                return new AddTagAction(key, val, modify);
 
123
        }
 
124
 
 
125
        private boolean inActionCmd() {
 
126
                boolean end = scanner.checkToken(";");
 
127
                return inAction() && !end;
 
128
        }
 
129
 
 
130
        private boolean inAction() {
 
131
                return !scanner.isEndOfFile() && !scanner.checkToken("}");
 
132
        }
 
133
}