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