1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24
25 /*
26  * Contributors:
27  * Greg Hilton 
28  */

29
30 package net.sf.jasperreports.engine.export;
31
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35
36 import net.sf.jasperreports.annotations.properties.Property;
37 import net.sf.jasperreports.annotations.properties.PropertyScope;
38 import net.sf.jasperreports.engine.JRAbstractExporter;
39 import net.sf.jasperreports.engine.JRPrintElement;
40 import net.sf.jasperreports.engine.JRPropertiesUtil;
41 import net.sf.jasperreports.export.Exporter;
42 import net.sf.jasperreports.properties.PropertyConstants;
43
44 /**
45  * @author Teodor Danciu (teodord@users.sourceforge.net)
46  */

47 public class MatcherExporterFilter implements ExporterFilter
48 {
49
50     /**
51      *
52      */

53     @Property(
54             category = PropertyConstants.CATEGORY_EXPORT,
55             scopes = {PropertyScope.ELEMENT},
56             sinceVersion = PropertyConstants.VERSION_5_5_0
57             )
58     public static final String PROPERTY_MATCHER_EXPORT_FILTER_KEY = JRPropertiesUtil.PROPERTY_PREFIX + "export.matcher.filter.key";
59
60     private Set<String> includes;
61     private Set<String> excludes;
62
63     @Override
64     public boolean isToExport(JRPrintElement element)
65     {
66         if (element.hasProperties() && element.getPropertiesMap().containsProperty(PROPERTY_MATCHER_EXPORT_FILTER_KEY))
67         {
68             String matcherKey = element.getPropertiesMap().getProperty(PROPERTY_MATCHER_EXPORT_FILTER_KEY);
69             if (matcherKey != null)
70             {
71                 if (includes == null || includes.size() == 0)
72                 {
73                     if (excludes == null || excludes.size() == 0)
74                     {
75                         return true;
76                     }
77                     else
78                     {
79                         return !excludes.contains(matcherKey);
80                     }
81                 }
82                 else
83                 {
84                     if (excludes == null || excludes.size() == 0)
85                     {
86                         return includes.contains(matcherKey);
87                     }
88                     else
89                     {
90                         return includes.contains(matcherKey) && !excludes.contains(matcherKey);
91                     }
92                 }
93             }
94         }
95         return true;
96     }
97     
98     public MatcherExporterFilter(Set<String> includes, Set<String> excludes)
99     {
100         this.includes = includes;
101         this.excludes = excludes;
102     }
103     
104     public static MatcherExporterFilter getInstance(JRExporterContext exporterContext)
105     {
106         MatcherExporterFilter filter = null;
107         
108         Exporter exporter = exporterContext.getExporterRef();
109         JRAbstractExporter typedExporter = exporter instanceof JRAbstractExporter ? (JRAbstractExporter)exporter : null;
110         
111         if (typedExporter != null)
112         {
113             String exporterKey = typedExporter.getExporterKey();
114             if (exporterKey != null)
115             {
116                 Set<String> includes = new HashSet<String>();
117                 Set<String> excludes = new HashSet<String>();
118                 List<MatcherExportFilterMapping> mappings = exporterContext.getJasperReportsContext().getExtensions(MatcherExportFilterMapping.class);
119                 for (MatcherExportFilterMapping mapping : mappings)
120                 {
121                     if (exporterKey.equals(mapping.getExporterKey()))
122                     {
123                         if (mapping.isIncludes())
124                         {
125                             includes.add(mapping.getValue());
126                         }
127                         else
128                         {
129                             excludes.add(mapping.getValue());
130                         }
131                     }
132                 }
133                 
134                 filter = new MatcherExporterFilter(includes, excludes);
135             }
136         }
137         
138         return filter;
139     }
140 }
141