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

File DefaultMacroIdFactory.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
12
1
1
81
38
4
0.33
12
1
4

Classes

Class Line # Actions
DefaultMacroIdFactory 40 12 0% 4 1
0.941176594.1%
 

Contributing tests

This file is covered by 4 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.internal.macro;
21   
22    import javax.inject.Inject;
23    import javax.inject.Singleton;
24   
25    import org.xwiki.component.annotation.Component;
26    import org.xwiki.rendering.macro.MacroId;
27    import org.xwiki.rendering.macro.MacroIdFactory;
28    import org.xwiki.rendering.parser.ParseException;
29    import org.xwiki.rendering.syntax.Syntax;
30    import org.xwiki.rendering.syntax.SyntaxFactory;
31   
32    /**
33    * Default implementation for {@link org.xwiki.rendering.macro.MacroIdFactory}.
34    *
35    * @version $Id: 767010dd2956afa304fe0374092cbebce33ea851 $
36    * @since 2.0M3
37    */
38    @Component
39    @Singleton
 
40    public class DefaultMacroIdFactory implements MacroIdFactory
41    {
42    /**
43    * Error message when the macro id format is invalid.
44    */
45    private static final String INVALID_MACRO_ID_FORMAT = "Invalid macro id format [%s]";
46   
47    /**
48    * For creating Syntax objects when creating MacroId from a string representing the syntax id and the
49    * syntax version.
50    */
51    @Inject
52    private SyntaxFactory syntaxFactory;
53   
 
54  56 toggle @Override
55    public MacroId createMacroId(String macroIdAsString) throws ParseException
56    {
57  56 MacroId macroId;
58   
59    // Verify if we have a syntax specified.
60  56 String[] hintParts = macroIdAsString.split("/");
61  56 if (hintParts.length == 3) {
62    // We've found a macro id for a macro that should be available only for a given syntax
63  3 Syntax syntax;
64  3 try {
65  3 syntax = this.syntaxFactory.createSyntaxFromIdString(
66    String.format("%s/%s", hintParts[1], hintParts[2]));
67    } catch (ParseException e) {
68  0 throw new ParseException(String.format(INVALID_MACRO_ID_FORMAT, macroIdAsString), e);
69    }
70  3 macroId = new MacroId(hintParts[0], syntax);
71  53 } else if (hintParts.length == 1) {
72    // We've found a macro registered for all syntaxes
73  52 macroId = new MacroId(macroIdAsString);
74    } else {
75    // Invalid format
76  1 throw new ParseException(String.format(INVALID_MACRO_ID_FORMAT, macroIdAsString));
77    }
78   
79  55 return macroId;
80    }
81    }