1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.syntax

File Syntax.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

14
30
11
1
318
117
18
0.6
2.73
11
1.64

Classes

Class Line # Actions
Syntax 38 30 0% 18 6
0.890909189.1%
 

Contributing tests

No tests hitting this source file were found.

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.syntax;
21   
22    import java.util.regex.Matcher;
23    import java.util.regex.Pattern;
24   
25    import org.apache.commons.lang3.builder.CompareToBuilder;
26    import org.apache.commons.lang3.builder.EqualsBuilder;
27    import org.apache.commons.lang3.builder.HashCodeBuilder;
28    import org.xwiki.rendering.parser.ParseException;
29   
30    /**
31    * Represents a wiki syntax that the user can use to enter wiki content. A syntax is made of two parts: a type (eg
32    * XWiki, Confluence, MediaWiki, etc) and a version (1.0, 2.0, etc). For example the XWiki 1.0 syntax, the XWiki 2.0
33    * syntax, the Confluence 1.0 syntax, etc.
34    *
35    * @version $Id: ce01bf9c7a3f59d1659a22f9744198dd3dac1976 $
36    * @since 2.0RC1
37    */
 
38    public class Syntax implements Comparable<Syntax>
39    {
40    /**
41    * HTML5 syntax.
42    *
43    * @since 6.4M3
44    */
45    public static final Syntax HTML_5_0 = new Syntax(SyntaxType.HTML, "5.0");
46   
47    /**
48    * XHTML 1.0 syntax.
49    */
50    public static final Syntax XHTML_1_0 = new Syntax(SyntaxType.XHTML, "1.0");
51   
52    /**
53    * HTML 4.01 syntax.
54    */
55    public static final Syntax HTML_4_01 = new Syntax(SyntaxType.HTML, "4.01");
56   
57    /**
58    * XWiki 1.0 syntax.
59    *
60    * @deprecated since 5.0, use {@link #XWIKI_2_1} instead
61    */
62    @Deprecated
63    public static final Syntax XWIKI_1_0 = new Syntax(SyntaxType.XWIKI, "1.0");
64   
65    /**
66    * XWiki 2.0 syntax.
67    */
68    public static final Syntax XWIKI_2_0 = new Syntax(SyntaxType.XWIKI, "2.0");
69   
70    /**
71    * XWiki 2.1 syntax.
72    */
73    public static final Syntax XWIKI_2_1 = new Syntax(SyntaxType.XWIKI, "2.1");
74   
75    /**
76    * Plain text syntax.
77    */
78    public static final Syntax PLAIN_1_0 = new Syntax(SyntaxType.PLAIN, "1.0");
79   
80    /**
81    * Events syntax.
82    */
83    public static final Syntax EVENT_1_0 = new Syntax(SyntaxType.EVENT, "1.0");
84   
85    /**
86    * TEX syntax.
87    */
88    public static final Syntax TEX_1_0 = new Syntax(SyntaxType.TEX, "1.0");
89   
90    /**
91    * Creole syntax.
92    */
93    public static final Syntax CREOLE_1_0 = new Syntax(SyntaxType.CREOLE, "1.0");
94   
95    /**
96    * JSPWiki syntax.
97    */
98    public static final Syntax JSPWIKI_1_0 = new Syntax(SyntaxType.JSPWIKI, "1.0");
99   
100    /**
101    * Old MediaWiki syntax.
102    *
103    * @deprecated since 8.2RC1, use {@link #MEDIAWIKI_1_6} instead
104    */
105    public static final Syntax MEDIAWIKI_1_0 = new Syntax(SyntaxType.MEDIAWIKI, "1.0");
106   
107    /**
108    * New MediaWiki syntax.
109    */
110    public static final Syntax MEDIAWIKI_1_6 = new Syntax(SyntaxType.MEDIAWIKI, "1.6");
111   
112    /**
113    * DokuWiki syntax.
114    *
115    * @since 9.8RC1
116    */
117    public static final Syntax DOKUWIKI_1_0 = new Syntax(SyntaxType.DOKUWIKI, "1.0");
118   
119    /**
120    * TWiki syntax.
121    */
122    public static final Syntax TWIKI_1_0 = new Syntax(SyntaxType.TWIKI, "1.0");
123   
124    /**
125    * Docbook 4.4 syntax.
126    */
127    public static final Syntax DOCBOOK_4_4 = new Syntax(SyntaxType.DOCBOOK, "4.4");
128   
129    /**
130    * Confluence wiki syntax.
131    */
132    public static final Syntax CONFLUENCE_1_0 = new Syntax(SyntaxType.CONFLUENCE, "1.0");
133   
134    /**
135    * Confluence XHTML based syntax.
136    *
137    * @since 5.3M1
138    */
139    public static final Syntax CONFLUENCEXHTML_1_0 = new Syntax(SyntaxType.CONFLUENCEXHTML, "1.0");
140   
141    /**
142    * @since 3.3M1
143    */
144    public static final Syntax XDOMXML_CURRENT = new Syntax(SyntaxType.XDOMXML, "current");
145   
146    /**
147    * @since 3.3M1
148    */
149    public static final Syntax XDOMXML_1_0 = new Syntax(SyntaxType.XDOMXML, "1.0");
150   
151    /**
152    * @since 3.4M1
153    */
154    public static final Syntax MARKDOWN_1_0 = new Syntax(SyntaxType.MARKDOWN, "1.0");
155   
156    /**
157    * @since 5.2M1
158    */
159    public static final Syntax MARKDOWN_1_1 = new Syntax(SyntaxType.MARKDOWN, "1.1");
160   
161    /**
162    * @since 4.3M1
163    */
164    public static final Syntax APT_1_0 = new Syntax(SyntaxType.APT, "1.0");
165   
166    /**
167    * This is HTML with annotations (comments) in order to allow round tripping between for example the WYSIWYG editor
168    * and wiki syntax.
169    */
170    public static final Syntax ANNOTATED_XHTML_1_0 = new Syntax(SyntaxType.ANNOTATED_XHTML, XHTML_1_0.getVersion());
171   
172    /**
173    * This is HTML5 with annotations (comments) in order to allow round tripping between for example the WYSIWYG editor
174    * and wiki syntax.
175    */
176    public static final Syntax ANNOTATED_HTML_5_0 = new Syntax(SyntaxType.ANNOTATED_HTML, HTML_5_0.getVersion());
177   
178    private static final long serialVersionUID = 1L;
179   
180    /**
181    * Used to cut the syntax identifier into syntax name and syntax version.
182    */
183    private static final Pattern SYNTAX_PATTERN = Pattern.compile("(.*)\\/(.*)");
184   
185    private SyntaxType type;
186   
187    private String version;
188   
189    /**
190    * Optional free form text that qualifies the version, eg "experimental".
191    */
192    private String qualifier;
193   
194    private transient volatile String idStringCache;
195   
196    /**
197    * @param type the type of the syntax
198    * @param version the specific version of the syntax
199    */
 
200  270460 toggle public Syntax(SyntaxType type, String version)
201    {
202  270579 this.type = type;
203  270585 this.version = version;
204    }
205   
206    /**
207    * @param type the type of the syntax
208    * @param version the specific version of the syntax
209    * @param qualifier a qualifier
210    */
 
211  0 toggle public Syntax(SyntaxType type, String version, String qualifier)
212    {
213  0 this(type, version);
214  0 this.qualifier = qualifier;
215    }
216   
217    /**
218    * @param syntaxIdAsString the syntax as a string (eg "xwiki/2.0", "plain/1.0")
219    * @return the parsed syntax as a Syntax object
220    * @throws ParseException in case the string doesn't represent a valid syntax
221    * @since 9.8RC1
222    */
 
223  267795 toggle public static Syntax valueOf(String syntaxIdAsString) throws ParseException
224    {
225  268215 if (syntaxIdAsString == null) {
226  1 throw new ParseException("The passed Syntax cannot be NULL");
227    }
228   
229  268370 Matcher matcher = SYNTAX_PATTERN.matcher(syntaxIdAsString);
230  268653 if (!matcher.matches()) {
231  751 throw new ParseException(String.format("Invalid Syntax format [%s]", syntaxIdAsString));
232    }
233   
234  267952 String syntaxId = matcher.group(1);
235  267892 String version = matcher.group(2);
236   
237    // For well-known syntaxes, get the Syntax Name from the registered SyntaxType, otherwise use the id as both
238    // the human readable name and the technical id (since the syntax string doesn't contain any information about
239    // the pretty name of a syntax type).
240  267809 SyntaxType syntaxType = SyntaxType.getSyntaxTypes().get(syntaxId);
241  267448 if (syntaxType == null) {
242  25 syntaxType = new SyntaxType(syntaxId, syntaxId);
243    }
244   
245  267646 return new Syntax(syntaxType, version);
246    }
247   
248    /**
249    * @return the type of the syntax
250    */
 
251  879637 toggle public SyntaxType getType()
252    {
253  879731 return this.type;
254    }
255   
256    /**
257    * @return the specific version of the syntax
258    */
 
259  372837 toggle public String getVersion()
260    {
261  372957 return this.version;
262    }
263   
264    /**
265    * @return a qualifier
266    */
 
267  184074 toggle public String getQualifier()
268    {
269  184075 return this.qualifier;
270    }
271   
272    /**
273    * @return a unique String identifier, does not contain display name and qualifier. Usable when searching for
274    * parsers and renderers components for example.
275    */
 
276  5463531 toggle public String toIdString()
277    {
278  5464059 if (this.idStringCache == null) {
279  188115 this.idStringCache = getType().getId() + "/" + getVersion().toLowerCase();
280    }
281   
282  5464215 return this.idStringCache;
283    }
284   
 
285  10287 toggle @Override
286    public String toString()
287    {
288  10287 return getType().toString() + " " + getVersion() + (getQualifier() != null ? " (" + getQualifier() + ")" : "");
289    }
290   
 
291  9 toggle @Override
292    public int hashCode()
293    {
294  9 return new HashCodeBuilder(5, 7).append(getType()).append(getVersion()).append(getQualifier()).toHashCode();
295    }
296   
 
297  87634 toggle @Override
298    public boolean equals(Object object)
299    {
300  87635 if (object == this) {
301  748 return true;
302    }
303  86888 if (!(object instanceof Syntax)) {
304  0 return false;
305    }
306  86888 Syntax rhs = (Syntax) object;
307  86888 return new EqualsBuilder().append(getType(), rhs.getType()).append(getVersion(), rhs.getVersion())
308    .append(getQualifier(), rhs.getQualifier()).isEquals();
309    }
310   
 
311  3 toggle @Override
312    public int compareTo(Syntax syntax)
313    {
314  3 return new CompareToBuilder().append(getType(), syntax.getType())
315    // TODO: Add a real version parser to compare the versions
316    .append(getVersion(), syntax.getVersion()).toComparison();
317    }
318    }