package word.utils; import java.io.BufferedReader; //import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import word.api.interfaces.IDocument; //import javax.xml.transform.Source; //import javax.xml.transform.Transformer; //import javax.xml.transform.TransformerFactory; //import javax.xml.transform.stream.StreamResult; //import javax.xml.transform.stream.StreamSource; //import org.dom4j.DocumentException; //import org.dom4j.DocumentHelper; //import org.dom4j.Node; public class Utils { /** * @return * * The root of the web app as String. */ public static String getAppRoot() { File file = new File("."); try { return file.getCanonicalPath(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Can't get app root directory", e); } } /** * * @param file * : It is the full path to the file * * @return String with the content of the file */ public static String readFile(String file) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { e.printStackTrace(); throw new RuntimeException("Can't find the file", e); } String line = null; StringBuilder stringBuilder = new StringBuilder(); String ls = System.getProperty("line.separator"); try { while ((line = reader.readLine()) != null) { stringBuilder.append(line); stringBuilder.append(ls); } return stringBuilder.toString(); } catch (IOException e) { e.printStackTrace(); return null; } finally { reader = null; } } public static String replaceSpecialCharacters(IDocument myDoc) { // String cont = myDoc.getContent(); // cont = cont.replace("ì", "ì"); // cont = cont.replace("í", "í"); // cont = cont.replace("î", "î"); // cont = cont.replace("ï", "ï"); System.out.println("#### FFF 02 ####"); System.out.println(myDoc.getContent()); return myDoc.getContent().replace("í", "í"); } /** * Full list: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references * @param original: the original string that may contain special characters * @return a new string that all specials have been replaced with Unicode Code Point(Decimal) */ public static String replaceSpecialCharacters(String original) { Map<String, String> specials = new HashMap<String, String>(); //from 192 to 255 specials.put("À", "À"); specials.put("Á", "Á"); specials.put("Â", "Â"); specials.put("Ã", "Ã"); specials.put("Ä", "Ä"); specials.put("Ä", "Ä"); specials.put("Å", "Å"); specials.put("Æ", "Æ"); specials.put("Ç", "Ç"); specials.put("È", "È"); specials.put("É", "É"); specials.put("Ê", "Ê"); specials.put("Ë", "Ë"); specials.put("Ì", "Ì"); specials.put("Í", "Í"); specials.put("Î", "Î"); specials.put("Ï", "Ï"); specials.put("Ð", "Ð"); specials.put("Ñ", "Ñ"); specials.put("Ò", "Ò"); specials.put("Ó", "Ó"); specials.put("Ô", "Ô"); specials.put("Õ", "Õ"); specials.put("Ö", "Ö"); specials.put("×", "×"); specials.put("Ø", "Ø"); specials.put("Ù", "Ù"); specials.put("Ú", "Ú"); specials.put("Û", "Û"); specials.put("Ü", "Ü"); specials.put("Ý", "Ý"); specials.put("Þ", "Þ"); specials.put("ß", "ß"); specials.put("à", "à"); specials.put("á", "á"); specials.put("â", "â"); specials.put("ã", "ã"); specials.put("ä", "ä"); specials.put("å", "å"); specials.put("æ", "æ"); specials.put("ç", "ç"); specials.put("è", "è"); specials.put("é", "é"); specials.put("ê", "ê"); specials.put("ë", "ë"); specials.put("ì", "ì"); specials.put("í", "í"); specials.put("î", "î"); specials.put("ï", "ï"); specials.put("ð", "ð"); specials.put("ñ", "ñ"); specials.put("ò", "ò"); specials.put("ó", "ó"); specials.put("ô", "ô"); specials.put("õ", "õ"); specials.put("ö", "ö"); specials.put("÷", "÷"); specials.put("ø", "ø"); specials.put("ù", "ù"); specials.put("ú", "ú"); specials.put("û", "û"); specials.put("ü", "ü"); specials.put("ý", "ý"); specials.put("þ", "þ"); specials.put("ÿ", "ÿ"); //from xx to xx, if we need more for (String key : specials.keySet()) { original = original.replace(key, specials.get(key)); } return original; } /** * * @param xml * xml to be pretifized * * @return pretifized xml */ /* * public static String pretty(String xml) { try { org.dom4j.Document * document = DocumentHelper.parseText(xml) .getDocument(); * * String res = formatXml(document, false); return res; } catch * (DocumentException e) { e.printStackTrace(); throw new * RuntimeException("Can't parse xml", e); } } */ /* * public static String formatXml(Node node, boolean oneLine) { * * try { TransformerFactory tf = TransformerFactory.newInstance(); * Transformer transformer = tf.newTransformer(); * * transformer.setOutputProperty("omit-xml-declaration", "yes"); * transformer.setOutputProperty("method", "xml"); * transformer.setOutputProperty("encoding", "ISO-8859-1"); * transformer.setOutputProperty( * "{http://xml.apache.org/xslt}indent-amount", "4"); * transformer.setOutputProperty("indent", "yes"); java.io.StringWriter sw = * new java.io.StringWriter(); StreamResult sr = new StreamResult(sw); // * Must strip out new lines and whitespace, because formatter thinks // this * is content that should be preserved in pretty String xml = * node.asXML().replaceAll(">\\s*(\\r|\\n)\\s*", ">") * .replaceAll("\\s*(\\r|\\n)\\s*<", "<"); * * // String xml = node.asXML(); ByteArrayInputStream inputStream = new * ByteArrayInputStream(xml .getBytes("UTF-8")); Source domSource = new * StreamSource(inputStream); transformer.transform(domSource, sr); * * String prettyXml = sw.toString(); * * // Although this looks like the same thing as above, it actually // isn't * // if (oneLine) { // prettyXml = * prettyXml.replaceAll(">\\s*(\\r|\\n)\\s*<", "><"); // } * * return prettyXml; } catch (Exception e) { throw new RuntimeException(e); * } } */ }