~w-shackleton/droidpad-android/stable

« back to all changes in this revision

Viewing changes to src/uk/digitalsquid/droidpad/layout/XmlDecoder.java

  • Committer: William Shackleton
  • Date: 2013-04-12 14:42:12 UTC
  • mfrom: (51.3.5 json)
  • mto: This revision was merged to the branch mainline in revision 52.
  • Revision ID: w.shackleton@gmail.com-20130412144212-4f7pueqwee0lc3yd
Imported code to load JSON layouts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package uk.digitalsquid.droidpad.xml;
2
 
 
3
 
import java.io.IOException;
4
 
import java.io.InputStream;
5
 
 
6
 
import javax.xml.parsers.ParserConfigurationException;
7
 
import javax.xml.parsers.SAXParser;
8
 
import javax.xml.parsers.SAXParserFactory;
9
 
 
10
 
import org.xml.sax.Attributes;
11
 
import org.xml.sax.ContentHandler;
12
 
import org.xml.sax.InputSource;
13
 
import org.xml.sax.SAXException;
14
 
 
15
 
import uk.digitalsquid.droidpad.LogTag;
16
 
import uk.digitalsquid.droidpad.buttons.Button;
17
 
import uk.digitalsquid.droidpad.buttons.Layout;
18
 
import uk.digitalsquid.droidpad.buttons.ModeSpec;
19
 
import uk.digitalsquid.droidpad.buttons.Slider;
20
 
import uk.digitalsquid.droidpad.buttons.ToggleButton;
21
 
import uk.digitalsquid.droidpad.buttons.TouchPanel;
22
 
import uk.digitalsquid.droidpad.buttons.Slider.SliderType;
23
 
import uk.digitalsquid.droidpad.buttons.TouchPanel.PanelType;
24
 
import android.sax.Element;
25
 
import android.sax.ElementListener;
26
 
import android.sax.RootElement;
27
 
import android.sax.TextElementListener;
28
 
 
29
 
 
30
 
public class LayoutDecoder implements LogTag {
31
 
        private LayoutDecoder() {}
 
1
/*  This file is part of DroidPad.
 
2
 *
 
3
 *  DroidPad is free software: you can redistribute it and/or modify
 
4
 *  it under the terms of the GNU General Public License as published by
 
5
 *  the Free Software Foundation, either version 3 of the License, or
 
6
 *  (at your option) any later version.
 
7
 *
 
8
 *  DroidPad 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
 *  You should have received a copy of the GNU General Public License
 
14
 *  along with DroidPad.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
package uk.digitalsquid.droidpad.layout;
 
18
 
 
19
import java.io.IOException;
 
20
import java.io.Reader;
 
21
 
 
22
import javax.xml.parsers.ParserConfigurationException;
 
23
import javax.xml.parsers.SAXParser;
 
24
import javax.xml.parsers.SAXParserFactory;
 
25
 
 
26
import org.xml.sax.Attributes;
 
27
import org.xml.sax.ContentHandler;
 
28
import org.xml.sax.InputSource;
 
29
import org.xml.sax.SAXException;
 
30
 
 
31
import uk.digitalsquid.droidpad.LogTag;
 
32
import uk.digitalsquid.droidpad.buttons.Button;
 
33
import uk.digitalsquid.droidpad.buttons.Layout;
 
34
import uk.digitalsquid.droidpad.buttons.ModeSpec;
 
35
import uk.digitalsquid.droidpad.buttons.Orientation;
 
36
import uk.digitalsquid.droidpad.buttons.Slider;
 
37
import uk.digitalsquid.droidpad.buttons.ToggleButton;
 
38
import uk.digitalsquid.droidpad.buttons.TouchPanel;
 
39
import android.sax.Element;
 
40
import android.sax.ElementListener;
 
41
import android.sax.RootElement;
 
42
import android.sax.TextElementListener;
 
43
 
 
44
 
 
45
public class XmlDecoder implements LogTag {
 
46
        private XmlDecoder() {}
32
47
        
33
48
        private static final SAXParserFactory factory = SAXParserFactory.newInstance();
34
49
        
35
 
        public static final ModeSpec decodeLayout(InputStream stream) throws IOException {
 
50
        public static final ModeSpec decodeLayout(Reader stream) throws IOException {
36
51
                try {
37
52
                        return internalDecodeLayout(stream);
38
53
                } catch (ParserConfigurationException e) {
45
60
                        throw e2;
46
61
                }
47
62
        }
48
 
        private static final ModeSpec internalDecodeLayout(InputStream stream) throws ParserConfigurationException, SAXException, IOException {
 
63
        private static final ModeSpec internalDecodeLayout(Reader stream) throws ParserConfigurationException, SAXException, IOException {
49
64
                SAXParser parser = factory.newSAXParser();
50
65
                
51
66
                DocumentListener doc = new DocumentListener();
163
178
                        slider.setElementListener(new ElementListener() {
164
179
                                
165
180
                                int x, y, width, height;
166
 
                                SliderType type;
 
181
                                Orientation type;
167
182
                                
168
183
                                @Override
169
184
                                public void start(Attributes attr) {
174
189
                                        
175
190
                                        String typeName = attr.getValue("type");
176
191
                                        if(typeName == null) typeName = "both";
177
 
                                        if(typeName.equalsIgnoreCase("x")) type = SliderType.X;
178
 
                                        if(typeName.equalsIgnoreCase("y")) type = SliderType.Y;
179
 
                                        if(typeName.equalsIgnoreCase("horizontal")) type = SliderType.X;
180
 
                                        if(typeName.equalsIgnoreCase("vertical")) type = SliderType.Y;
 
192
                                        if(typeName.equalsIgnoreCase("x")) type = Orientation.X;
 
193
                                        if(typeName.equalsIgnoreCase("y")) type = Orientation.Y;
 
194
                                        if(typeName.equalsIgnoreCase("horizontal")) type = Orientation.X;
 
195
                                        if(typeName.equalsIgnoreCase("vertical")) type = Orientation.Y;
181
196
                                        
182
 
                                        if(typeName.equalsIgnoreCase("xy")) type = SliderType.Both;
183
 
                                        if(typeName.equalsIgnoreCase("both")) type = SliderType.Both;
184
 
                                        if(typeName.equalsIgnoreCase("dual")) type = SliderType.Both;
 
197
                                        if(typeName.equalsIgnoreCase("xy")) type = Orientation.Both;
 
198
                                        if(typeName.equalsIgnoreCase("both")) type = Orientation.Both;
 
199
                                        if(typeName.equalsIgnoreCase("dual")) type = Orientation.Both;
185
200
                                }
186
201
                                
187
202
                                @Override
194
209
                        panel.setElementListener(new ElementListener() {
195
210
                                
196
211
                                int x, y, width, height;
197
 
                                PanelType type;
 
212
                                Orientation type;
198
213
                                
199
214
                                @Override
200
215
                                public void start(Attributes attr) {
205
220
                                        
206
221
                                        String typeName = attr.getValue("type");
207
222
                                        if(typeName == null) typeName = "both";
208
 
                                        if(typeName.equalsIgnoreCase("x")) type = PanelType.X;
209
 
                                        if(typeName.equalsIgnoreCase("y")) type = PanelType.Y;
210
 
                                        if(typeName.equalsIgnoreCase("horizontal")) type = PanelType.X;
211
 
                                        if(typeName.equalsIgnoreCase("vertical")) type = PanelType.Y;
 
223
                                        if(typeName.equalsIgnoreCase("x")) type = Orientation.X;
 
224
                                        if(typeName.equalsIgnoreCase("y")) type = Orientation.Y;
 
225
                                        if(typeName.equalsIgnoreCase("horizontal")) type = Orientation.X;
 
226
                                        if(typeName.equalsIgnoreCase("vertical")) type = Orientation.Y;
212
227
                                        
213
 
                                        if(typeName.equalsIgnoreCase("xy")) type = PanelType.Both;
214
 
                                        if(typeName.equalsIgnoreCase("both")) type = PanelType.Both;
215
 
                                        if(typeName.equalsIgnoreCase("dual")) type = PanelType.Both;
 
228
                                        if(typeName.equalsIgnoreCase("xy")) type = Orientation.Both;
 
229
                                        if(typeName.equalsIgnoreCase("both")) type = Orientation.Both;
 
230
                                        if(typeName.equalsIgnoreCase("dual")) type = Orientation.Both;
216
231
                                }
217
232
                                
218
233
                                @Override