Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources.setup

File InstallTextResourceTask.java

 

Coverage histogram

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

Code metrics

2
22
10
1
121
61
12
0.55
2.2
10
1.2

Classes

Class Line # Actions
InstallTextResourceTask 49 22 0% 12 7
0.794117679.4%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.resources.setup;
35   
36    import info.magnolia.cms.util.ClasspathResourcesUtil;
37    import info.magnolia.module.InstallContext;
38    import info.magnolia.module.delta.TaskExecutionException;
39    import info.magnolia.module.resources.ResourceTypes;
40   
41    import java.io.InputStream;
42   
43    import org.apache.commons.io.IOUtils;
44    import org.apache.commons.lang3.StringUtils;
45   
46    /**
47    * Task to load single non-binary resource to the repository.
48    */
 
49    public class InstallTextResourceTask extends AbstractInstallResourceTask {
50   
51    public static final String DEFAULT_ENCODING = "UTF-8";
52   
53    protected String modelClass;
54    private String encoding = DEFAULT_ENCODING;
55   
 
56  0 toggle public InstallTextResourceTask(String resource) {
57  0 this(resource, true);
58    }
59   
 
60  1 toggle public InstallTextResourceTask(String resource, boolean processed) {
61  1 this(resource, processed, DEFAULT_ENCODING);
62    }
63   
 
64  2 toggle public InstallTextResourceTask(String resource, boolean processed, String encoding) {
65  2 this(resource, ResourceTypes.determineType(resource, processed), encoding);
66    }
67   
 
68  1 toggle public InstallTextResourceTask(String resource, String template) {
69  1 this(resource, template, DEFAULT_ENCODING);
70    }
71   
 
72  3 toggle public InstallTextResourceTask(String resource, String template, String encoding) {
73  3 this(resource, template, encoding, null, null, null, null, false);
74    }
75   
 
76  0 toggle public InstallTextResourceTask(String resource, boolean processed, String version, String source, String rights, String modelClass, boolean preserveResourceParameters) {
77  0 this(resource, ResourceTypes.determineType(resource, processed), DEFAULT_ENCODING, version, source, rights, modelClass, preserveResourceParameters);
78    }
79   
 
80  0 toggle public InstallTextResourceTask(String resource, boolean processed, String encoding, String version, String source, String rights, String modelClass, boolean preserveResourceParameters) {
81  0 this(resource, ResourceTypes.determineType(resource, processed), encoding, version, source, rights, modelClass, preserveResourceParameters);
82    }
83   
 
84  4 toggle public InstallTextResourceTask(String resource, String template, String encoding, String version, String source, String rights, String modelClass, boolean preserveResourceParameters) {
85  4 this(resource, template, encoding, version, source, rights, modelClass, preserveResourceParameters, true);
86    }
87   
 
88  8 toggle public InstallTextResourceTask(String resource, String template, String encoding, String version, String source, String rights, String modelClass, boolean preserveResourceParameters, boolean stripExtension) {
89  8 super(resource, template, StringUtils.contains(template, ResourceTypes.PROCESSED_PREFIX), version, source, rights, preserveResourceParameters, stripExtension);
90  8 this.modelClass = modelClass;
91  8 this.encoding = encoding;
92    }
93   
 
94  8 toggle @Override
95    public void execute(InstallContext installContext) throws TaskExecutionException {
96   
97  8 super.execute(installContext);
98   
99  7 try {
100  7 if (this.preserveResourceParameters) {
101  1 this.modelClass = this.getProperty("modelClass");
102    }
103   
104    // set additionally meta information
105  7 this.setProperty("modelClass", modelClass);
106   
107  7 InputStream stream = ClasspathResourcesUtil.getStream(resource);
108   
109  7 try {
110  7 String content = IOUtils.toString(stream, encoding);
111  7 this.setProperty("text", content);
112    } finally {
113  7 IOUtils.closeQuietly(stream);
114    }
115    } catch (Exception e) {
116  0 throw new TaskExecutionException("Can't install resource [" + resource + "]", e);
117    }
118   
119    }
120   
121    }