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

File AbstractEditor.java

 

Coverage histogram

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

Code metrics

0
10
2
1
73
34
2
0.2
5
2
1

Classes

Class Line # Actions
AbstractEditor 39 10 0% 2 0
1.0100%
 

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.edit;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.script.ScriptContext;
27   
28    import org.xwiki.script.ScriptContextManager;
29    import org.xwiki.stability.Unstable;
30   
31    /**
32    * Base class for editors that take their input from the {@link ScriptContext}.
33    *
34    * @param <D> the type of data that can be edited by this editor
35    * @version $Id: b6c412be68d209cfc2fbc6768e406b36e24b41fe $
36    * @since 8.2RC1
37    */
38    @Unstable
 
39    public abstract class AbstractEditor<D> implements Editor<D>
40    {
41    /**
42    * The key used to access the edit information from the script context.
43    */
44    private static final String EDIT_CONTEXT_KEY = "edit";
45   
46    @Inject
47    private ScriptContextManager scripts;
48   
 
49  337 toggle @Override
50    public String render(D data, Map<String, Object> parameters) throws EditException
51    {
52  337 ScriptContext scriptContext = this.scripts.getCurrentScriptContext();
53   
54  337 Object currentEdit = scriptContext.getAttribute(EDIT_CONTEXT_KEY, ScriptContext.ENGINE_SCOPE);
55  337 try {
56  337 scriptContext.setAttribute(EDIT_CONTEXT_KEY, getEditContext(data, parameters), ScriptContext.ENGINE_SCOPE);
57   
58  337 return render();
59    } finally {
60  337 scriptContext.setAttribute(EDIT_CONTEXT_KEY, currentEdit, ScriptContext.ENGINE_SCOPE);
61    }
62    }
63   
 
64  337 toggle protected Map<String, Object> getEditContext(D data, Map<String, Object> parameters)
65    {
66  337 Map<String, Object> editContext = new HashMap<>();
67  337 editContext.put("data", data);
68  337 editContext.put("parameters", parameters);
69  337 return editContext;
70    }
71   
72    protected abstract String render() throws EditException;
73    }