001    //$HeadURL: https://sushibar/svn/deegree/base/trunk/resources/eclipse/svn_classfile_header_template.xml $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    
037    package org.deegree.io.mapinfoapi;
038    
039    import static org.deegree.io.mapinfoapi.MapInfoReader.parseCommaList;
040    
041    import java.io.File;
042    import java.io.IOException;
043    import java.io.StreamTokenizer;
044    import java.util.HashMap;
045    import java.util.LinkedList;
046    
047    /**
048     * <code>MIFStyleParser</code>
049     *
050     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
051     * @author last edited by: $Author:$
052     *
053     * @version $Revision:$, $Date:$
054     */
055    public class MIFStyleParser {
056    
057        private StreamTokenizer mif;
058    
059        private File baseFile;
060    
061        /**
062         * @param mif
063         * @param baseFile
064         *            for resolving relative file names
065         */
066        public MIFStyleParser( StreamTokenizer mif, File baseFile ) {
067            this.mif = mif;
068            this.baseFile = baseFile;
069        }
070    
071        /**
072         * @return a map with the symbol parameters, or null, if no symbol clause was found
073         * @throws IOException
074         */
075        public HashMap<String, String> parseSymbol()
076                                throws IOException {
077            // ignore style
078            if ( mif.sval != null && mif.sval.equals( "symbol" ) ) {
079                LinkedList<String> list = parseCommaList( mif );
080    
081                HashMap<String, String> map = new HashMap<String, String>();
082    
083                if ( list.size() == 3 ) {
084                    // MapInfo 3.0 syntax
085                    map.put( "shape", list.poll() );
086                    map.put( "color", list.poll() );
087                    map.put( "size", list.poll() );
088                    return map;
089                }
090    
091                if ( list.size() == 6 ) {
092                    // ttf font syntax
093                    map.put( "shape", list.poll() );
094                    map.put( "color", list.poll() );
095                    map.put( "size", list.poll() );
096                    map.put( "fontname", list.poll() );
097                    map.put( "fontstyle", list.poll() );
098                    map.put( "rotation", list.poll() );
099                    return map;
100                }
101    
102                if ( list.size() == 4 ) {
103                    // custom bitmap file
104                    map.put( "filename", new File( baseFile, list.poll() ).getAbsolutePath() );
105                    map.put( "color", list.poll() );
106                    map.put( "size", list.poll() );
107                    map.put( "customstyle", list.poll() );
108                    return map;
109                }
110            }
111    
112            return null;
113        }
114    
115        /**
116         * @return a map with the pen parameters, or null, if no pen clause was found
117         * @throws IOException
118         */
119        public HashMap<String, String> parsePen()
120                                throws IOException {
121            if ( mif.sval != null && mif.sval.equals( "pen" ) ) {
122                LinkedList<String> list = parseCommaList( mif );
123    
124                HashMap<String, String> map = new HashMap<String, String>();
125    
126                if ( list.size() == 3 ) {
127                    map.put( "width", list.poll() );
128                    map.put( "pattern", list.poll() );
129                    map.put( "color", list.poll() );
130                }
131    
132                return map;
133            }
134    
135            return null;
136        }
137    
138        /**
139         * @return a map with the brush parameters, or null, if no brush clause was found
140         * @throws IOException
141         */
142        public HashMap<String, String> parseBrush()
143                                throws IOException {
144            if ( mif.sval != null && mif.sval.equals( "brush" ) ) {
145                LinkedList<String> list = parseCommaList( mif );
146                HashMap<String, String> map = new HashMap<String, String>();
147    
148                map.put( "pattern", list.poll() );
149                map.put( "forecolor", list.poll() );
150                if ( list.size() > 0 ) {
151                    map.put( "backcolor", list.poll() );
152                }
153    
154                return map;
155            }
156    
157            return null;
158        }
159    
160        /**
161         * @return a map with the brush parameters, or null, if no brush clause was found
162         * @throws IOException
163         */
164        public HashMap<String, String> parseText()
165                                throws IOException {
166            HashMap<String, String> map = new HashMap<String, String>();
167    
168            if ( mif.sval != null && mif.sval.equals( "font" ) ) {
169                LinkedList<String> list = parseCommaList( mif );
170    
171                map.put( "fontname", list.poll() );
172                map.put( "style", list.poll() );
173                map.put( "size", list.poll() );
174                map.put( "forecolor", list.poll() );
175                if ( list.size() > 0 ) {
176                    map.put( "backcolor", list.poll() );
177                }
178            }
179    
180            if ( mif.sval != null && mif.sval.equals( "spacing" ) ) {
181                mif.nextToken();
182                mif.nextToken();
183            }
184    
185            if ( mif.sval != null && mif.sval.equals( "justify" ) ) {
186                mif.nextToken();
187                mif.nextToken();
188            }
189    
190            if ( mif.sval != null && mif.sval.equals( "angle" ) ) {
191                mif.nextToken();
192                mif.nextToken();
193            }
194    
195            if ( mif.sval != null && mif.sval.equals( "label" ) ) {
196                mif.nextToken();
197                mif.nextToken(); // line
198                mif.nextToken(); // simple/arrow
199                mif.nextToken(); // x
200                mif.nextToken(); // y
201            }
202    
203            if ( map.isEmpty() ) {
204                return null;
205            }
206            return map;
207        }
208    
209    }