1
24
25
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
47 public class MatcherExporterFilter implements ExporterFilter
48 {
49
50
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