1 package fr.orsay.lri.varna.utils;
5 import java.util.ArrayList;
6 import java.util.Formatter;
8 import javax.xml.transform.sax.TransformerHandler;
10 import org.xml.sax.Attributes;
11 import org.xml.sax.SAXException;
12 import org.xml.sax.helpers.AttributesImpl;
14 import fr.orsay.lri.varna.models.rna.ModeleBase;
15 import fr.orsay.lri.varna.models.rna.RNA;
17 public class XMLUtils {
18 public static String toHTMLNotation(Color c)
20 Formatter f = new Formatter();
21 f.format("#%02X%02X%02X", c.getRed(),c.getGreen(),c.getBlue());
25 public static void toXML(TransformerHandler hd, Font f) throws SAXException
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";
39 public static void toXML(TransformerHandler hd, Font f, String role) throws SAXException
41 AttributesImpl atts = new AttributesImpl();
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);
52 public static Font getFont(String qName, Attributes attributes)
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);
65 public static void toXML(TransformerHandler hd, ModeleBase mb) throws SAXException
67 ArrayList<ModeleBase> m = new ArrayList<ModeleBase>();
73 public static void toXML(TransformerHandler hd, ArrayList<ModeleBase> m) throws SAXException
75 AttributesImpl atts = new AttributesImpl();
77 for (ModeleBase mb: m)
79 if (!result.equals(""))
81 result += mb.getIndex();
84 hd.startElement("","",XML_BASELIST_ELEMENT_NAME,atts);
85 exportCDATAString(hd, result);
86 hd.endElement("","",XML_BASELIST_ELEMENT_NAME);
89 public static ArrayList<ModeleBase> toModeleBaseArray(String baselist, RNA rna)
91 ArrayList<ModeleBase> result = new ArrayList<ModeleBase>();
92 String[] data = baselist.trim().split(",");
93 for(int i=0;i<data.length;i++)
95 int index = Integer.parseInt(data[i]);
96 result.add(rna.getBaseAt(index));
102 public static void exportCDATAElem(TransformerHandler hd,String elem, String s) throws SAXException
104 char[] t = s.toCharArray();
105 AttributesImpl atts = new AttributesImpl();
106 hd.startElement("","",elem,atts);
108 hd.characters(t, 0, t.length);
110 hd.endElement("","",elem);
113 public static void exportCDATAString(TransformerHandler hd, String s) throws SAXException
115 char[] t = s.toCharArray();
117 hd.characters(t, 0, t.length);
120 public static boolean getBoolean(Attributes attributes, String attName, boolean defVal)
122 String val = attributes.getValue(attName);
125 return Boolean.parseBoolean(val);
129 public static int getInt(Attributes attributes, String attName, int defVal)
131 String val = attributes.getValue(attName);
134 return Integer.parseInt(val);
138 public static double getDouble(Attributes attributes, String attName, double defVal)
140 String val = attributes.getValue(attName);
143 return Double.parseDouble(val);