~ubuntu-branches/ubuntu/trusty/freeguide/trusty

« back to all changes in this revision

Viewing changes to src/freeguide/build/preparedata/PrepareHallmarkInfo.java

  • Committer: Bazaar Package Importer
  • Author(s): Shaun Jackman
  • Date: 2007-09-11 16:52:59 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070911165259-4r32oke21i1ezbmv
Tags: 0.10.5-1
* New upstream release.
* Update the watch file.
* Change Debian policy to version 3.7.2.2. No changes necessary.
* Add ant-optional to build dependencies. Closes: #441762.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package freeguide.build.preparedata;
 
2
 
 
3
import freeguide.common.lib.general.StringHelper;
 
4
import freeguide.common.lib.grabber.HtmlHelper;
 
5
import freeguide.common.lib.grabber.HttpBrowser;
 
6
 
 
7
import org.w3c.dom.Document;
 
8
import org.w3c.dom.Element;
 
9
 
 
10
import org.xml.sax.Attributes;
 
11
import org.xml.sax.SAXException;
 
12
 
 
13
import java.io.File;
 
14
 
 
15
import java.text.MessageFormat;
 
16
 
 
17
import java.util.Arrays;
 
18
import java.util.Map;
 
19
import java.util.TimeZone;
 
20
import java.util.TreeMap;
 
21
import java.util.regex.Matcher;
 
22
import java.util.regex.Pattern;
 
23
 
 
24
import javax.xml.parsers.DocumentBuilderFactory;
 
25
import javax.xml.transform.Transformer;
 
26
import javax.xml.transform.TransformerFactory;
 
27
import javax.xml.transform.dom.DOMSource;
 
28
import javax.xml.transform.stream.StreamResult;
 
29
 
 
30
/**
 
31
 * Prepare information about all hallmark sites.
 
32
 *
 
33
 * @author Alex Buloichik (mailto: alex73 at zaval.org)
 
34
 */
 
35
public class PrepareHallmarkInfo
 
36
{
 
37
    protected static final Pattern RE_CNTRY_URL =
 
38
        Pattern.compile( "http://([a-z]{2}).hallmarkchannel.com" );
 
39
    protected static final Pattern RE_LANG =
 
40
        Pattern.compile( "LANG=([A-Z0-9_]+)" );
 
41
    protected static final String STR_CNTRY_USA =
 
42
        "http://www.hallmarkchannel.com";
 
43
    protected static final String URL_CHOOSE_COUNTRY =
 
44
        "http://www.hallmarkchannel.com/chooseCountry.jsp";
 
45
    protected static final String SRC_INFO_FILE_PATH =
 
46
        "src/resources/plugins/grabber/hallmark/info.xml";
 
47
    protected static final String UTF8_CHARSET = "UTF-8";
 
48
 
 
49
    /**
 
50
     * DOCUMENT_ME!
 
51
     *
 
52
     * @param args DOCUMENT_ME!
 
53
     *
 
54
     * @throws Exception DOCUMENT_ME!
 
55
     */
 
56
    public static void main( final String[] args ) throws Exception
 
57
    {
 
58
        String[] timezones = TimeZone.getAvailableIDs(  );
 
59
        Arrays.sort( timezones );
 
60
 
 
61
        for( String tz : timezones )
 
62
        {
 
63
            System.out.println( tz );
 
64
        }
 
65
 
 
66
        final HttpBrowser browser = new HttpBrowser(  );
 
67
        browser.loadURL( URL_CHOOSE_COUNTRY );
 
68
 
 
69
        HandlerCountries countries = new HandlerCountries(  );
 
70
        browser.parse( countries );
 
71
        System.out.println(  );
 
72
 
 
73
        Document doc =
 
74
            DocumentBuilderFactory.newInstance(  ).newDocumentBuilder(  )
 
75
                                  .newDocument(  );
 
76
        final Element docHallmark = doc.createElement( "hallmark" );
 
77
        doc.appendChild( docHallmark );
 
78
 
 
79
        int i = 1;
 
80
 
 
81
        for( final Map.Entry<String, String> entry : countries.countries
 
82
            .entrySet(  ) )
 
83
        {
 
84
            String country = entry.getKey(  );
 
85
            String url = entry.getValue(  );
 
86
 
 
87
            System.out.println( 
 
88
                MessageFormat.format( 
 
89
                    "Country {0} ({1}/{2}) - {3}", country, i,
 
90
                    countries.countries.size(  ), url ) );
 
91
 
 
92
            final String id = getCntry( url );
 
93
            System.out.println( "url = " + url + "   cntry = " + id );
 
94
 
 
95
            if( id == null )
 
96
            {
 
97
                System.out.println( "Error read url: " + url );
 
98
 
 
99
                continue;
 
100
            }
 
101
 
 
102
            final Element docCountry = doc.createElement( "country" );
 
103
            docCountry.setAttribute( "id", id );
 
104
            docCountry.setAttribute( "country", country );
 
105
            docCountry.setAttribute( "url", url );
 
106
 
 
107
            for( final Map.Entry<String, String> lang : getLanguages( url, id )
 
108
                                                            .entrySet(  ) )
 
109
            {
 
110
                final Element docLanguage = doc.createElement( "language" );
 
111
                docLanguage.setAttribute( "name", lang.getKey(  ) );
 
112
                docLanguage.setAttribute( "id", lang.getValue(  ) );
 
113
                docCountry.appendChild( docLanguage );
 
114
            }
 
115
 
 
116
            docHallmark.appendChild( docCountry );
 
117
            i++;
 
118
        }
 
119
 
 
120
        final Transformer xformer =
 
121
            TransformerFactory.newInstance(  ).newTransformer(  );
 
122
        xformer.setOutputProperty( "indent", "yes" );
 
123
 
 
124
        xformer.transform( 
 
125
            new DOMSource( doc ),
 
126
            new StreamResult( new File( SRC_INFO_FILE_PATH ) ) );
 
127
 
 
128
    }
 
129
 
 
130
    protected static String getCntry( final String url )
 
131
        throws Exception
 
132
    {
 
133
        Matcher m = RE_CNTRY_URL.matcher( url );
 
134
 
 
135
        if( m.matches(  ) )
 
136
        {
 
137
            return m.group( 1 ).toUpperCase(  );
 
138
        }
 
139
        else
 
140
        {
 
141
            if( STR_CNTRY_USA.equals( url ) )
 
142
            {
 
143
                return "US";
 
144
            }
 
145
            else
 
146
            {
 
147
                return null;
 
148
            }
 
149
        }
 
150
    }
 
151
 
 
152
    protected static Map<String, String> getLanguages( 
 
153
        final String url, final String cntry ) throws Exception
 
154
    {
 
155
        final HttpBrowser browser = new HttpBrowser(  );
 
156
        browser.loadURL( 
 
157
            url + "/framework.jsp?BODY=weekSchedCal.jsp&CNTRY=" + cntry );
 
158
 
 
159
        HandlerLanguages h = new HandlerLanguages(  );
 
160
        browser.parse( h );
 
161
 
 
162
        Map<String, String> langs = h.getLanguages(  );
 
163
        Map<String, String> result = new TreeMap<String, String>(  );
 
164
 
 
165
        for( final Map.Entry<String, String> entry : langs.entrySet(  ) )
 
166
        {
 
167
            Matcher m = RE_LANG.matcher( entry.getValue(  ) );
 
168
 
 
169
            if( m.find(  ) )
 
170
            {
 
171
                result.put( entry.getKey(  ), m.group( 1 ) );
 
172
            }
 
173
            else
 
174
            {
 
175
                System.err.println( "Invalid language: " + entry.getValue(  ) );
 
176
            }
 
177
        }
 
178
 
 
179
        /*HallmarkParserSchedule parserTimeZone =
 
180
            new HallmarkParserSchedule( null, null, cntry.equals( "US" ) );
 
181
        browser.parse( parserTimeZone );*/
 
182
        return result;
 
183
    }
 
184
 
 
185
    protected static class HandlerCountries extends HtmlHelper.DefaultContentHandler
 
186
    {
 
187
        Map<String, String> countries = new TreeMap<String, String>(  );
 
188
        protected boolean process = false;
 
189
        protected String currentOptionValue;
 
190
        protected StringBuffer currentText = new StringBuffer(  );
 
191
 
 
192
        /**
 
193
         * DOCUMENT_ME!
 
194
         *
 
195
         * @param uri DOCUMENT_ME!
 
196
         * @param localName DOCUMENT_ME!
 
197
         * @param qName DOCUMENT_ME!
 
198
         * @param atts DOCUMENT_ME!
 
199
         *
 
200
         * @throws SAXException DOCUMENT_ME!
 
201
         */
 
202
        public void startElement( 
 
203
            String uri, String localName, String qName, Attributes atts )
 
204
            throws SAXException
 
205
        {
 
206
            if( 
 
207
                "select".equals( qName )
 
208
                    && "CNTRY".equals( atts.getValue( "name" ) ) )
 
209
            {
 
210
                process = true;
 
211
            }
 
212
            else if( process && "option".equals( qName ) )
 
213
            {
 
214
                currentOptionValue = atts.getValue( "value" );
 
215
                currentText.setLength( 0 );
 
216
 
 
217
                if( 
 
218
                    ( currentOptionValue == null )
 
219
                        || StringHelper.EMPTY_STRING.equals( 
 
220
                            currentOptionValue )
 
221
                        || !currentOptionValue.endsWith( 
 
222
                            "hallmarkchannel.com" ) )
 
223
                {
 
224
                    currentOptionValue = null;
 
225
                }
 
226
            }
 
227
        }
 
228
 
 
229
        /**
 
230
         * DOCUMENT_ME!
 
231
         *
 
232
         * @param uri DOCUMENT_ME!
 
233
         * @param localName DOCUMENT_ME!
 
234
         * @param qName DOCUMENT_ME!
 
235
         *
 
236
         * @throws SAXException DOCUMENT_ME!
 
237
         */
 
238
        public void endElement( String uri, String localName, String qName )
 
239
            throws SAXException
 
240
        {
 
241
            if( "select".equals( qName ) )
 
242
            {
 
243
                process = false;
 
244
            }
 
245
            else if( 
 
246
                process && "option".equals( qName )
 
247
                    && ( currentOptionValue != null ) )
 
248
            {
 
249
                countries.put( currentText.toString(  ), currentOptionValue );
 
250
                currentOptionValue = null;
 
251
            }
 
252
        }
 
253
 
 
254
        /**
 
255
         * DOCUMENT_ME!
 
256
         *
 
257
         * @param ch DOCUMENT_ME!
 
258
         * @param start DOCUMENT_ME!
 
259
         * @param length DOCUMENT_ME!
 
260
         *
 
261
         * @throws SAXException DOCUMENT_ME!
 
262
         */
 
263
        public void characters( char[] ch, int start, int length )
 
264
            throws SAXException
 
265
        {
 
266
            if( currentOptionValue != null )
 
267
            {
 
268
                currentText.append( ch, start, length );
 
269
            }
 
270
        }
 
271
    }
 
272
 
 
273
    protected static class HandlerLanguages extends HtmlHelper.DefaultContentHandler
 
274
    {
 
275
        protected Map<String, String> languages =
 
276
            new TreeMap<String, String>(  );
 
277
        protected boolean process = false;
 
278
        protected String currentOptionValue;
 
279
        protected StringBuffer currentText = new StringBuffer(  );
 
280
 
 
281
        /**
 
282
         * DOCUMENT_ME!
 
283
         *
 
284
         * @param uri DOCUMENT_ME!
 
285
         * @param localName DOCUMENT_ME!
 
286
         * @param qName DOCUMENT_ME!
 
287
         * @param atts DOCUMENT_ME!
 
288
         *
 
289
         * @throws SAXException DOCUMENT_ME!
 
290
         */
 
291
        public void startElement( 
 
292
            String uri, String localName, String qName, Attributes atts )
 
293
            throws SAXException
 
294
        {
 
295
            if( 
 
296
                "select".equals( qName )
 
297
                    && "LANG".equals( atts.getValue( "name" ) ) )
 
298
            {
 
299
                process = true;
 
300
            }
 
301
            else if( process && "option".equals( qName ) )
 
302
            {
 
303
                currentOptionValue = atts.getValue( "value" );
 
304
                currentText.setLength( 0 );
 
305
 
 
306
                if( 
 
307
                    ( currentOptionValue == null )
 
308
                        || StringHelper.EMPTY_STRING.equals( 
 
309
                            currentOptionValue ) )
 
310
                {
 
311
                    currentOptionValue = null;
 
312
                }
 
313
            }
 
314
        }
 
315
 
 
316
        /**
 
317
         * DOCUMENT_ME!
 
318
         *
 
319
         * @param uri DOCUMENT_ME!
 
320
         * @param localName DOCUMENT_ME!
 
321
         * @param qName DOCUMENT_ME!
 
322
         *
 
323
         * @throws SAXException DOCUMENT_ME!
 
324
         */
 
325
        public void endElement( String uri, String localName, String qName )
 
326
            throws SAXException
 
327
        {
 
328
            if( "select".equals( qName ) )
 
329
            {
 
330
                process = false;
 
331
            }
 
332
            else if( 
 
333
                process && "option".equals( qName )
 
334
                    && ( currentOptionValue != null ) )
 
335
            {
 
336
                languages.put( 
 
337
                    currentText.toString(  ).trim(  ), currentOptionValue );
 
338
                currentOptionValue = null;
 
339
            }
 
340
        }
 
341
 
 
342
        /**
 
343
         * DOCUMENT_ME!
 
344
         *
 
345
         * @param ch DOCUMENT_ME!
 
346
         * @param start DOCUMENT_ME!
 
347
         * @param length DOCUMENT_ME!
 
348
         *
 
349
         * @throws SAXException DOCUMENT_ME!
 
350
         */
 
351
        public void characters( char[] ch, int start, int length )
 
352
            throws SAXException
 
353
        {
 
354
            if( currentOptionValue != null )
 
355
            {
 
356
                currentText.append( ch, start, length );
 
357
            }
 
358
        }
 
359
 
 
360
        /**
 
361
         * DOCUMENT_ME!
 
362
         *
 
363
         * @return DOCUMENT_ME!
 
364
         */
 
365
        public Map<String, String> getLanguages(  )
 
366
        {
 
367
            return languages;
 
368
        }
 
369
    }
 
370
}