1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimodel.util

File WikiEntityUtil.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

12
76
9
2
204
117
15
0.2
8.44
4.5
1.67

Classes

Class Line # Actions
WikiEntityUtil 29 73 0% 14 25
0.731182873.1%
WikiEntityUtil.Entity 31 3 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 78 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.wikimodel.util;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    /**
26    * @version $Id: f4b0419ecd0b310eeb8a30497f330b1c12478c99 $
27    * @since 4.0M1
28    */
 
29    public class WikiEntityUtil
30    {
 
31    private static class Entity
32    {
33    public final int fHtmlCode;
34   
35    public final String fHtmlSymbol;
36   
37    public final String fWikiSymbol;
38   
 
39  57 toggle public Entity(String wikiSymbol, String htmlSymbol, int htmlCode)
40    {
41  57 fWikiSymbol = wikiSymbol;
42  57 fHtmlSymbol = htmlSymbol;
43  57 fHtmlCode = htmlCode;
44    }
45    }
46   
47    private static Map<String, Entity> fHtmlToWiki = new HashMap<String, Entity>();
48   
49    private static Entity[] fIdToWiki = new Entity[65535];
50   
51    private static Map<String, Entity> fWikiToHtml = new HashMap<String, Entity>();
52   
 
53  1 toggle static {
54  1 add("<", "lt", 8249);
55  1 add(">", "gt", 8250);
56  1 add("&", "amp", 38); // ???
57   
58  1 add("\'", "rsquo", 8217);
59  1 add("(tm)", "trade", 8482);
60  1 add("(TM)", "trade", 8482);
61  1 add("(No)", "8470", 8470);
62  1 add(" -- ", "ndash", 8211);
63  1 add("---", "mdash", 8212);
64  1 add(" --- ", "mdash", 8212);
65  1 add("...", "hellip", 8230);
66  1 add("(*)", "bull", 8226);
67  1 add("(R)", "reg", 174);
68  1 add("(r)", "reg", 174);
69  1 add("(o)", "deg", 176);
70  1 add("(C)", "copy", 169);
71  1 add("(p)", "para", 182);
72  1 add("(P)", "para", 182);
73  1 add("(s)", "sect", 167);
74  1 add("()", "nbsp", 160);
75  1 add("<<", "laquo", 171);
76  1 add(">>", "raquo", 187);
77    // add("<", "lsaquo", 8249);
78    // add(">", "rsaquo", 8250);
79   
80    // Currency
81  1 add("(c)", "cent", 162);
82  1 add("(E)", "euro", 8364);
83  1 add("(O)", "curren", 164);
84  1 add("(L)", "pound", 163);
85  1 add("(Y)", "yen", 165);
86  1 add("(f)", "fnof", 402);
87   
88    // Math
89  1 add("+/-", "plusmn", 177);
90  1 add("(S)", "sum", 8721);
91  1 add("(/)", "divide", 247);
92  1 add("(x)", "times", 215);
93  1 add("(8)", "infin", 8734);
94  1 add("(~)", "sim", 8764);
95  1 add("!=", "ne", 8800);
96   
97  1 add("->", "rarr", 8594);
98  1 add("-->", "rarr", 8594);
99  1 add("--->", "rarr", 8594);
100   
101  1 add("<-", "larr", 8592);
102  1 add("<--", "larr", 8592);
103  1 add("<---", "larr", 8592);
104   
105  1 add("<->", "harr", 8596);
106  1 add("<-->", "harr", 8596);
107  1 add("<--->", "harr", 8596);
108   
109  1 add("=>", "rArr", 8658);
110  1 add("==>", "rArr", 8658);
111  1 add("===>", "rArr", 8658);
112   
113  1 add("<=", "lArr", 8658);
114  1 add("<==", "lArr", 8658);
115  1 add("<===", "lArr", 8658);
116   
117  1 add("<=>", "hArr", 8660);
118  1 add("<==>", "hArr", 8660);
119  1 add("<===>", "hArr", 8660);
120   
121  1 add("<=", "le", 8804);
122  1 add(">=", "ge", 8805);
123  1 add("!=", "ne", 8800);
124  1 add("~=", "asymp", 8776);
125    }
126   
 
127  57 toggle private static void add(String wikiEnity, String htmlEntity, int id)
128    {
129  57 Entity entity = new Entity(wikiEnity, htmlEntity, id);
130  57 fWikiToHtml.put(wikiEnity, entity);
131  57 fHtmlToWiki.put(htmlEntity, entity);
132  57 fIdToWiki[id] = entity;
133    }
134   
135    /**
136    * Returns an HTML code corresponding to the specified HTML entity.
137    *
138    * @param htmlEntity the HTML entity to transform to the corresponding HTML
139    * code
140    * @return an HTML code corresponding to the specified HTML entity
141    */
 
142  0 toggle public static int getHtmlCodeByHtmlEntity(String htmlEntity)
143    {
144  0 Entity entity = fHtmlToWiki.get(htmlEntity);
145  0 return entity != null ? entity.fHtmlCode : 0;
146    }
147   
148    /**
149    * Returns an HTML code corresponding to the specified wiki entity.
150    *
151    * @param wikiEntity the wiki entity to transform to the corresponding HTML
152    * entity
153    * @return an HTML code corresponding to the specified wiki entity
154    */
 
155  0 toggle public static int getHtmlCodeByWikiSymbol(String wikiEntity)
156    {
157  0 Entity entity = fWikiToHtml.get(wikiEntity);
158  0 return entity != null ? entity.fHtmlCode : 0;
159    }
160   
161    /**
162    * @param ch for this character the corresponding html entity will be
163    * returned
164    * @return an html entity corresponding to the given character
165    */
 
166  0 toggle public static String getHtmlSymbol(char ch)
167    {
168  0 Entity entity = fIdToWiki[ch];
169  0 return entity != null ? entity.fWikiSymbol : null;
170    }
171   
172    /**
173    * @param wikiEntity for this wiki entity the corresponding html entity will
174    * be returned
175    * @return an html entity corresponding to the given wiki symbol
176    */
 
177  599 toggle public static String getHtmlSymbol(String wikiEntity)
178    {
179  599 Entity entity = fWikiToHtml.get(wikiEntity);
180  599 return entity != null ? entity.fHtmlSymbol : null;
181    }
182   
183    /**
184    * @param ch for this character the corresponding wiki entity will be
185    * returned
186    * @return an wiki entity corresponding to the given character
187    */
 
188  0 toggle public static String getWikiSymbol(char ch)
189    {
190  0 Entity entity = fIdToWiki[ch];
191  0 return entity != null ? entity.fWikiSymbol : null;
192    }
193   
194    /**
195    * @param htmlEntity for this html entity the corresponding wiki entity will
196    * be returned
197    * @return an wiki entity corresponding to the given html symbol
198    */
 
199  0 toggle public static String getWikiSymbol(String htmlEntity)
200    {
201  0 Entity entity = fHtmlToWiki.get(htmlEntity);
202  0 return entity != null ? entity.fHtmlSymbol : null;
203    }
204    }