001/* 
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Cognium Systems SA - initial API and implementation
011 *******************************************************************************/
012package org.nuxeo.ecm.platform.rendering.wiki;
013
014import java.util.HashMap;
015import java.util.Map;
016
017/**
018 * Copied from last version of wikimodel
019 *
020 * @author kotelnikov
021 */
022public class WikiEntityUtil {
023
024    // Utility class.
025    private WikiEntityUtil() {
026    }
027
028    private static class Entity {
029
030        public final int fHtmlCode;
031
032        public final String fHtmlSymbol;
033
034        public final String fWikiSymbol;
035
036        private Entity(String wikiSymbol, String htmlSymbol, int htmlCode) {
037            fWikiSymbol = wikiSymbol;
038            fHtmlSymbol = htmlSymbol;
039            fHtmlCode = htmlCode;
040        }
041
042    }
043
044    private static final Map<String, Entity> fHtmlToWiki = new HashMap<String, Entity>();
045
046    private static final Entity[] fIdToWiki = new Entity[65535];
047
048    private static final Map<String, Entity> fWikiToHtml = new HashMap<String, Entity>();
049
050    static {
051        add("<", "lt", 8249);
052        add(">", "gt", 8250);
053        add("&", "amp", 38); // ???
054
055        add("\'", "rsquo", 8217);
056        add("(tm)", "trade", 8482);
057        add("(TM)", "trade", 8482);
058        add("(No)", "8470", 8470);
059        add(" -- ", "ndash", 8211);
060        add("---", "mdash", 8212);
061        add(" --- ", "mdash", 8212);
062        add("...", "hellip", 8230);
063        add("(*)", "bull", 8226);
064        add("(R)", "reg", 174);
065        add("(r)", "reg", 174);
066        add("(o)", "deg", 176);
067        add("(C)", "copy", 169);
068        add("(p)", "para", 182);
069        add("(P)", "para", 182);
070        add("(s)", "sect", 167);
071        add("()", "nbsp", 160);
072        add("<<", "laquo", 171);
073        add(">>", "raquo", 187);
074        // add("<", "lsaquo", 8249);
075        // add(">", "rsaquo", 8250);
076
077        // Currency
078        add("(c)", "cent", 162);
079        add("(E)", "euro", 8364);
080        add("(O)", "curren", 164);
081        add("(L)", "pound", 163);
082        add("(Y)", "yen", 165);
083        add("(f)", "fnof", 402);
084
085        // Math
086        add("+/-", "plusmn", 177);
087        add("(S)", "sum", 8721);
088        add("(/)", "divide", 247);
089        add("(x)", "times", 215);
090        add("(8)", "infin", 8734);
091        add("(~)", "sim", 8764);
092        add("!=", "ne", 8800);
093
094        add("->", "rarr", 8594);
095        add("-->", "rarr", 8594);
096        add("--->", "rarr", 8594);
097
098        add("<-", "larr", 8592);
099        add("<--", "larr", 8592);
100        add("<---", "larr", 8592);
101
102        add("<->", "harr", 8596);
103        add("<-->", "harr", 8596);
104        add("<--->", "harr", 8596);
105
106        add("=>", "rArr", 8658);
107        add("==>", "rArr", 8658);
108        add("===>", "rArr", 8658);
109
110        add("<=", "lArr", 8658);
111        add("<==", "lArr", 8658);
112        add("<===", "lArr", 8658);
113
114        add("<=>", "hArr", 8660);
115        add("<==>", "hArr", 8660);
116        add("<===>", "hArr", 8660);
117
118        add("<=", "le", 8804);
119        add(">=", "ge", 8805);
120        add("!=", "ne", 8800);
121        add("~=", "asymp", 8776);
122    }
123
124    private static void add(String wikiEnity, String htmlEntity, int id) {
125        Entity entity = new Entity(wikiEnity, htmlEntity, id);
126        fWikiToHtml.put(wikiEnity, entity);
127        fHtmlToWiki.put(htmlEntity, entity);
128        fIdToWiki[id] = entity;
129    }
130
131    /**
132     * Returns an HTML code corresponding to the specified HTML entity.
133     *
134     * @param htmlEntity the HTML entity to transform to the corresponding HTML code
135     * @return an HTML code corresponding to the specified HTML entity
136     */
137    public static int getHtmlCodeByHtmlEntity(String htmlEntity) {
138        Entity entity = fHtmlToWiki.get(htmlEntity);
139        return entity != null ? entity.fHtmlCode : 0;
140    }
141
142    /**
143     * Returns an HTML code corresponding to the specified wiki entity.
144     *
145     * @param wikiEntity the wiki entity to transform to the corresponding HTML entity
146     * @return an HTML code corresponding to the specified wiki entity
147     */
148    public static int getHtmlCodeByWikiSymbol(String wikiEntity) {
149        Entity entity = fWikiToHtml.get(wikiEntity);
150        return entity != null ? entity.fHtmlCode : 0;
151    }
152
153    /**
154     * @param ch for this character the corresponding html entity will be returned
155     * @return an html entity corresponding to the given character
156     */
157    public static String getHtmlSymbol(char ch) {
158        Entity entity = fIdToWiki[ch];
159        return entity != null ? entity.fWikiSymbol : null;
160    }
161
162    /**
163     * @param wikiEntity for this wiki entity the corresponding html entity will be returned
164     * @return an html entity corresponding to the given wiki symbol
165     */
166    public static String getHtmlSymbol(String wikiEntity) {
167        Entity entity = fWikiToHtml.get(wikiEntity);
168        return entity != null ? entity.fHtmlSymbol : null;
169    }
170
171    /**
172     * @param ch for this character the corresponding wiki entity will be returned
173     * @return an wiki entity corresponding to the given character
174     */
175    public static String getWikiSymbol(char ch) {
176        Entity entity = fIdToWiki[ch];
177        return entity != null ? entity.fWikiSymbol : null;
178    }
179
180    /**
181     * @param htmlEntity for this html entity the corresponding wiki entity will be returned
182     * @return an wiki entity corresponding to the given html symbol
183     */
184    public static String getWikiSymbol(String htmlEntity) {
185        Entity entity = fHtmlToWiki.get(htmlEntity);
186        return entity != null ? entity.fHtmlSymbol : null;
187    }
188
189}