PropertyFacetExtractor.java
4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFactory
* javax.jcr.nodetype.PropertyDefinition
*/
package com.day.cq.search.facets.extractors;
import com.day.cq.search.facets.FacetExtractor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.nodetype.PropertyDefinition;
public abstract class PropertyFacetExtractor
implements FacetExtractor {
protected static final List<Property> EMPTY_PROPERTY_LIST = Collections.emptyList();
protected final String propertyRelPath;
public PropertyFacetExtractor(String propertyRelPath) {
this.propertyRelPath = propertyRelPath;
}
protected abstract void handleValue(Value var1) throws RepositoryException;
protected abstract List<Value> filter(List<Value> var1, ValueFactory var2) throws RepositoryException;
@Override
public void handleNode(Node node) throws RepositoryException {
List<Value> values = this.getValues(node);
for (Value value : values) {
this.handleValue(value);
}
}
protected List<Value> getValues(Node node) throws RepositoryException {
ValueFactory vf = node.getSession().getValueFactory();
ArrayList<Value> values = null;
Iterator<Property> props = PropertyFacetExtractor.getProperties(node, this.propertyRelPath);
if (props.hasNext()) {
boolean isBinary = false;
values = new ArrayList<E>();
while (props.hasNext()) {
Property p = props.next();
if (p.getType() == 2) {
isBinary = true;
long[] lengths = p.getDefinition().isMultiple() ? p.getLengths() : new long[]{p.getLength()};
for (long len : lengths) {
values.add(vf.createValue(len));
}
continue;
}
if (p.getDefinition().isMultiple()) {
values.addAll(Arrays.asList(p.getValues()));
continue;
}
values.add(p.getValue());
}
if (isBinary) {
return values;
}
} else {
try {
if (node.hasNode(this.propertyRelPath)) {
values = new ArrayList<E>();
values.add(node.getSession().getValueFactory().createValue(node.getNode(this.propertyRelPath).getPath()));
}
}
catch (RepositoryException e) {
// empty catch block
}
}
if (values == null) {
values = Collections.emptyList();
}
return this.filter(values, vf);
}
private static Iterator<Property> getProperties(Node node, String pathPattern) throws RepositoryException {
if (pathPattern == null || pathPattern.equals(".")) {
return EMPTY_PROPERTY_LIST.iterator();
}
ArrayList<Node> nodes = new ArrayList<Node>();
ArrayList<Property> properties = new ArrayList<Property>();
nodes.add(node);
String[] namePattern = pathPattern.split("/");
for (int i = 0; i < namePattern.length; ++i) {
if (i == namePattern.length - 1) {
for (Node n : nodes) {
PropertyIterator pIt = n.getProperties(namePattern[i]);
while (pIt.hasNext()) {
properties.add(pIt.nextProperty());
}
}
continue;
}
ArrayList<Node> children = new ArrayList<Node>();
for (Node n : nodes) {
NodeIterator nIt = n.getNodes(namePattern[i]);
while (nIt.hasNext()) {
children.add(nIt.nextNode());
}
}
nodes = children;
}
return properties.iterator();
}
}