JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / utils / XMLUtils.java
1 package fr.orsay.lri.varna.utils;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.util.ArrayList;
6 import java.util.Formatter;
7
8 import javax.xml.transform.sax.TransformerHandler;
9
10 import org.xml.sax.Attributes;
11 import org.xml.sax.SAXException;
12 import org.xml.sax.helpers.AttributesImpl;
13
14 import fr.orsay.lri.varna.models.rna.ModeleBase;
15 import fr.orsay.lri.varna.models.rna.RNA;
16
17 public class XMLUtils {
18         public static String toHTMLNotation(Color c)
19         {
20                 Formatter f = new Formatter();
21                 f.format("#%02X%02X%02X", c.getRed(),c.getGreen(),c.getBlue());
22                 return f.toString();
23         }
24
25         public static void toXML(TransformerHandler hd, Font f) throws SAXException
26         {
27                 toXML(hd, f,"");
28         }
29         
30
31         public static String XML_BASELIST_ELEMENT_NAME = "baselist";
32         public static String XML_FONT_ELEMENT_NAME = "font";
33         public static String XML_ROLE_NAME = "role";
34         public static String XML_NAME_NAME = "name";
35         public static String XML_FAMILY_NAME = "family";
36         public static String XML_STYLE_NAME = "style";
37         public static String XML_SIZE_NAME = "size";
38         
39         public static void toXML(TransformerHandler hd, Font f, String role) throws SAXException
40         {
41                 AttributesImpl atts = new AttributesImpl();
42                 if (!role.equals(""))
43                   atts.addAttribute("","",XML_ROLE_NAME,"CDATA",""+role);
44                 atts.addAttribute("","",XML_NAME_NAME,"CDATA",""+f.getName());
45                 //atts.addAttribute("","",XML_FAMILY_NAME,"CDATA",""+f.getFamily());
46                 atts.addAttribute("","",XML_STYLE_NAME,"CDATA",""+f.getStyle());
47                 atts.addAttribute("","",XML_SIZE_NAME,"CDATA",""+f.getSize2D());
48                 hd.startElement("","",XML_FONT_ELEMENT_NAME,atts);
49                 hd.endElement("","",XML_FONT_ELEMENT_NAME);
50         }
51
52         public static Font getFont(String qName, Attributes attributes)
53         {
54                 if (qName.equals(XMLUtils.XML_FONT_ELEMENT_NAME)){
55                         int style = Integer.parseInt(attributes.getValue(XMLUtils.XML_STYLE_NAME));                     
56                         String name = (attributes.getValue(XMLUtils.XML_NAME_NAME));                    
57                         double size = Double.parseDouble(attributes.getValue(XMLUtils.XML_SIZE_NAME));
58                         Font f = new Font(name, style, (int)size);
59                         return f.deriveFont((float)size);
60                 }
61                 return null;
62         }
63         
64
65         public static void toXML(TransformerHandler hd, ModeleBase mb) throws SAXException
66         {
67                 ArrayList<ModeleBase> m = new ArrayList<ModeleBase>();
68                 m.add(mb);
69                 toXML(hd, m);
70         }
71
72
73         public static void toXML(TransformerHandler hd, ArrayList<ModeleBase> m) throws SAXException
74         {               
75                 AttributesImpl atts = new AttributesImpl();
76                 String result = "";
77                 for (ModeleBase mb: m)
78                 {
79                         if (!result.equals(""))
80                                 result+= ",";
81                         result += mb.getIndex();
82                                         
83                 }
84                 hd.startElement("","",XML_BASELIST_ELEMENT_NAME,atts);
85                 exportCDATAString(hd, result);
86                 hd.endElement("","",XML_BASELIST_ELEMENT_NAME);
87         }
88
89         public static ArrayList<ModeleBase> toModeleBaseArray(String baselist, RNA rna)
90         {
91                 ArrayList<ModeleBase> result = new ArrayList<ModeleBase>();
92                 String[] data = baselist.trim().split(",");
93                 for(int i=0;i<data.length;i++)
94                 {
95                         int index = Integer.parseInt(data[i]);
96                         result.add(rna.getBaseAt(index));
97                 }
98                         
99                 return result;
100         }
101         
102         public static void exportCDATAElem(TransformerHandler hd,String elem, String s) throws SAXException
103         {
104                 char[] t = s.toCharArray();
105                 AttributesImpl atts = new AttributesImpl();
106                 hd.startElement("","",elem,atts);
107                 hd.startCDATA();
108                 hd.characters(t, 0, t.length);
109                 hd.endCDATA();
110                 hd.endElement("","",elem);
111         }
112         
113         public static void exportCDATAString(TransformerHandler hd, String s) throws SAXException
114         {
115                 char[] t = s.toCharArray();
116                 hd.startCDATA();
117                 hd.characters(t, 0, t.length);
118                 hd.endCDATA();
119         }
120         public static boolean getBoolean(Attributes attributes, String attName, boolean defVal)
121         {
122                 String val = attributes.getValue(attName);
123                 if (val!=null)
124                 {
125                         return Boolean.parseBoolean(val);
126                 }
127                 return defVal;
128         }
129         public static int getInt(Attributes attributes, String attName, int defVal)
130         {
131                 String val = attributes.getValue(attName);
132                 if (val!=null)
133                 {
134                         return Integer.parseInt(val);
135                 }
136                 return defVal;
137         }
138         public static double getDouble(Attributes attributes, String attName, double defVal)
139         {
140                 String val = attributes.getValue(attName);
141                 if (val!=null)
142                 {
143                         return Double.parseDouble(val);
144                 }
145                 return defVal;
146         }
147         
148 }