Das client files
[jalview.git] / src / org / biojava / dasobert / das / DAS_StylesheetRetrieve.java
1 /*
2  *                  BioJava development code
3  *
4  * This code may be freely distributed and modified under the
5  * terms of the GNU Lesser General Public Licence.  This should
6  * be distributed with the code.  If you do not have a copy,
7  * see:
8  *
9  *      http://www.gnu.org/copyleft/lesser.html
10  *
11  * Copyright for this code is held jointly by the individual
12  * authors.  These should be listed in @author doc comments.
13  *
14  * For more information on the BioJava project and its aims,
15  * or to join the biojava-l mailing list, visit the home page
16  * at:
17  *
18  *      http://www.biojava.org/
19  *
20  * Created on Aug 3, 2005
21  *
22  */
23 package org.biojava.dasobert.das;
24
25 import java.io.InputStream;
26 import java.net.*;
27 import java.util.*;
28 import java.util.logging.Logger;
29
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.parsers.SAXParser;
32 import javax.xml.parsers.SAXParserFactory;
33
34 import org.xml.sax.InputSource;
35 import org.xml.sax.XMLReader;
36
37 /** this stores the stylesheet config for a DAS source.
38  *
39  * @author Andreas Prlic
40  *
41  */
42 public class DAS_StylesheetRetrieve {
43     static Logger logger = Logger.getLogger("org.biojava.spice");
44     /**
45      *
46      */
47     Map[] t3DMap;
48     public DAS_StylesheetRetrieve() {
49         super();
50
51     }
52
53     /** retrieve a StyleSheet from a URL
54      * The style sheet is represented as a Map[],
55      *  where each Map contains the description of how to draw a features of a particular type.
56      *
57      *  */
58     public Map[] retrieve(URL dasStylesheetRequest){
59
60         logger.finest("requesting stylesheet from " + dasStylesheetRequest);
61
62         InputStream inStream = null;
63
64                 try {
65                     HttpURLConnection huc = DAS_FeatureRetrieve.openHttpURLConnection(dasStylesheetRequest);
66
67                     logger.finest("got connection: "+huc.getResponseMessage());
68                     //String contentEncoding = huc.getContentEncoding();
69                     inStream = huc.getInputStream();
70
71
72                     SAXParserFactory spfactory =
73                             SAXParserFactory.newInstance();
74
75                         spfactory.setValidating(false);
76
77                     SAXParser saxParser = null ;
78
79                         try{
80                             saxParser =
81                                 spfactory.newSAXParser();
82                         } catch (ParserConfigurationException e) {
83                             e.printStackTrace();
84                         }
85
86                         DAS_Stylesheet_Handler cont_handle = new DAS_Stylesheet_Handler() ;
87                         XMLReader xmlreader = saxParser.getXMLReader();
88
89                         xmlreader.setContentHandler(cont_handle);
90                         xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
91                         InputSource insource = new InputSource() ;
92                         insource.setByteStream(inStream);
93
94
95                     xmlreader.parse(insource);
96                         Map[] typeMap = cont_handle.getTypeStyles();
97
98                         t3DMap = cont_handle.get3DStyles();
99                         return typeMap;
100
101                 } catch (Exception e) {
102                     logger.finest(e.getMessage());
103                     return null;
104                 }
105     }
106
107
108     public Map[] get3DStyle(){
109         return t3DMap;
110     }
111 }