/* * File : JDomUtility.java * Created : 14-jun-2001 17:25 * By : fbusquets * * JClic - Authoring and playing system for educational activities * * Copyright (C) 2000 - 2005 Francesc Busquets & Departament * d'Educacio de la Generalitat de Catalunya * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details (see the LICENSE file). */ package edu.xtec.util; import java.awt.*; import java.util.*; import java.text.DateFormat; import java.io.OutputStream; /** * * @author Francesc Busquets (fbusquets@xtec.net) * @version 1.0 */ public class JDomUtility { private static org.jdom.input.SAXBuilder saxBuilder; private static org.jdom.output.XMLOutputter outputter; public static final String ID="id", IMAGE="image", NAME="name", TYPE="type", BGCOLOR="bgcolor", FORECOLOR="forecolor", MARGIN="margin", BORDER="border", POSITION="position", X="x", Y="y", P="p", CLASS="class"; private JDomUtility(){ } public static boolean addGenericAttribute(org.jdom.Element e, String key, Object v){ if(v==null) return false; Class cl=v.getClass(); if(e.getAttribute(key)!=null) return false; if(cl.isArray()) return false; if(cl.isAssignableFrom(String.class) || cl.isAssignableFrom(Integer.class) || cl.isAssignableFrom(Float.class) || cl.isAssignableFrom(Double.class) || cl.isAssignableFrom(Boolean.class)) e.setAttribute(key, v.toString()); else if(cl.isAssignableFrom(Color.class)) e.setAttribute(key, colorToString((Color)v)); else return false; return true; } // ---------------------------------------------------------------------- // Font conversion functions // ---------------------------------------------------------------------- public static final String FONT="font"; private static final String FAMILY="family", SIZE="size", BOLD="bold", ITALIC="italic"; public static org.jdom.Element fontToElement(java.awt.Font font){ org.jdom.Element e=new org.jdom.Element(FONT); e.setAttribute(FAMILY, font.getFamily()); e.setAttribute(SIZE, Integer.toString(font.getSize())); if(font.isBold()) e.setAttribute(BOLD, boolString(font.isBold())); if(font.isItalic()) e.setAttribute(ITALIC, boolString(font.isItalic())); return e; } public static Font elementToFont(org.jdom.Element e) throws Exception{ checkName(e, FONT); String family=getStringAttr(e, FAMILY, "default", false); int size=getIntAttr(e, SIZE, 12); int style=(getBoolAttr(e, BOLD, false) ? Font.BOLD : 0 ) | (getBoolAttr(e, ITALIC, false) ? Font.ITALIC : 0); return FontCheck.getValidFont(family, style, size); } // ---------------------------------------------------------------------- // Color conversion functions // ---------------------------------------------------------------------- public static String colorToString(Color color){ // be careful with alpha : high bits String s=Long.toHexString(0x100000000L|color.getRGB()).toUpperCase(); s=s.substring(s.length()-(color.getAlpha()==0xFF ? 6 : 8)); //s=s.substring(color.getAlpha()==0xFF ? 3 : 1); //if(s.length()>6) s=s.substring(s.length()-6); return "0x"+s; } public static final String[] HTML_COLOR_NAMES={ "Black", "Silver", "Gray", "White", "Maroon", "Red", "Purple", "Fuchsia", "Green", "Lime", "Olive", "Yellow", "Navy", "Blue", "Teal", "Aqua", "Pink", "Orange", "DarkGray" }; public static final Color[] HTML_COLORS={ Color.black, Color.lightGray, Color.gray, Color.white, new Color(128, 0, 0), Color.red, new Color(128, 0, 128), Color.magenta, new Color(0, 128, 0), Color.green, new Color(128, 128, 0), Color.yellow, new Color(0, 0, 128), Color.blue, new Color(0, 128, 128), Color.cyan, Color.pink, Color.orange, Color.darkGray }; public static Color stringToColor(String s) throws Exception{ //Color c=Color.black; Color color=null; if(s.startsWith("0x")){ //c=Color.decode(s); long v=Long.decode(s).longValue(); color=new Color((int)v, s.length()>8 || v>=0x1000000); } else{ for(int i=0; i0){ StringTokenizer st=new StringTokenizer(s, ","); if(st.hasMoreTokens()) result[0]=getStrIndexAttr(st.nextToken(), hAlignName, defaultValue[0]); if(st.hasMoreTokens()) result[1]=getStrIndexAttr(st.nextToken(), vAlignName, defaultValue[1]); } } return result; } public static void setAlignProp(org.jdom.Element e, String id, int[] align, boolean omitIfDefault){ if(e!=null && id!=null && align!=null && align.length==2){ if(!omitIfDefault || !isDefaultAlign(align)){ e.setAttribute(id, hAlignName[align[0]]+","+vAlignName[align[1]]); } } } public static boolean isDefaultAlign(int[] align){ return(align!=null && align[0]==ALIGN_MIDDLE && align[1]==ALIGN_MIDDLE); } // ---------------------------------------------------------------------- // conversion functions for paragraphs of plain text // ---------------------------------------------------------------------- public static void addParagraphs(org.jdom.Element parent, String childName, String text){ if(text!=null){ org.jdom.Element child=new org.jdom.Element(childName); setParagraphs(child, text); parent.addContent(child); } } public static void setParagraphs(org.jdom.Element e, String text){ if(text!=null){ StringTokenizer st=new StringTokenizer(text, "\n"); while(st.hasMoreTokens()){ e.addContent(new org.jdom.Element(P).setText(st.nextToken())); } } } public static String getParagraphs(org.jdom.Element e){ StringBuffer sb=null; if(e!=null){ java.util.Iterator itr = e.getChildren(P).iterator(); while (itr.hasNext()) { String s=((org.jdom.Element)itr.next()).getText(); if(sb==null) sb=new StringBuffer(s); else sb.append("\n").append(s); } } return (sb==null ? null : sb.substring(0)); } // ---------------------------------------------------------------------- // conversion functions for arrays // ---------------------------------------------------------------------- public static String intArrayToString(int[] v){ return intArrayToString(v, v.length); } public static String intArrayToString(int[] v, int numElements){ StringBuffer sb=new StringBuffer(numElements*4); for(int i=0; i=DEFAULT) state=DEFAULT; return BOOL_STR[state]; } public static int getTriStateAttr(org.jdom.Element e, String attr, int defaultValue) throws Exception{ return getStrIndexAttr(e, attr, BOOL_STR, defaultValue); } public static boolean checkTriState(int v){ return v>=FALSE && v<=DEFAULT; } // boolean and Boolean public static String boolString(boolean value){ return BOOL_STR[value ? TRUE : FALSE]; } public static boolean getBoolAttr(org.jdom.Element e, String attr, boolean defaultValue) { String s=e.getAttributeValue(attr); boolean result=defaultValue; if(s!=null){ if(s.equalsIgnoreCase(BOOL_STR[TRUE])) result=true; else if(s.equalsIgnoreCase(BOOL_STR[FALSE])) result=false; else throw new NumberFormatException("invalid boolean: " + s); } return result; } public static Boolean getBooleanAttr(org.jdom.Element e, String attr, Boolean defaultValue) { String s=e.getAttributeValue(attr); Boolean result=defaultValue; if(s!=null){ if(s.equalsIgnoreCase(BOOL_STR[TRUE]) || s.equalsIgnoreCase(BOOL_STR[FALSE])){ result=new Boolean(s); } else{ throw new NumberFormatException("invalid boolean: " + s); } } return result; } // float public static float getFloatAttr(org.jdom.Element e, String attr, float defaultValue) { String s=e.getAttributeValue(attr); float result=defaultValue; if(s!=null){ result=Float.parseFloat(s); } return result; } // double public static double getDoubleAttr(org.jdom.Element e, String attr, double defaultValue) { String s=e.getAttributeValue(attr); double result=defaultValue; if(s!=null){ result=Double.parseDouble(s); } return result; } // ---------------------------------------------------------------------- // conversion functions for basic classes // ---------------------------------------------------------------------- // String public static String getStringAttr(org.jdom.Element e, String attr, String defaultValue, boolean allowEmpty){ String s=e.getAttributeValue(attr); String result=defaultValue; if(s!=null && (allowEmpty || s.length()>0)) result=s; return result; } public static void setStringAttr(org.jdom.Element e, String key, String value, boolean allowEmpty){ String v = allowEmpty && (value==null || value.length()==0) ? "" : value; if(key!=null && v!=null) e.setAttribute(key, v); } // String array index public static int getStrIndexAttr(org.jdom.Element e, String attr, String[] values, int defaultValue) throws Exception{ return getStrIndexAttr(e.getAttributeValue(attr), values, defaultValue); } public static int getStrIndexAttr(String s, String[] values, int defaultValue) throws Exception{ int result=defaultValue; if(s!=null && s.length()>0){ for(result=0; result