blob: c6008ef4084722d754eb4eb204e14a3501d44d62 (
plain) (
blame)
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.content.cluster;
import com.yahoo.document.select.DocumentSelector;
import com.yahoo.document.select.parser.ParseException;
import com.yahoo.document.select.rule.DocumentNode;
import com.yahoo.vespa.model.content.DocumentTypeVisitor;
import com.yahoo.vespa.model.builder.xml.dom.ModelElement;
/**
* @author thomasg
*/
public class DocumentSelectionBuilder {
private static class AllowedDocumentTypesChecker extends DocumentTypeVisitor {
String allowedType;
private AllowedDocumentTypesChecker(String allowedType) {
this.allowedType = allowedType;
}
@Override
public void visit(DocumentNode documentNode) {
if (!documentNode.getType().equals(this.allowedType)) {
if (this.allowedType == null) {
throw new IllegalArgumentException("Document type references are not allowed " +
"in global <documents> tag selection attribute (found reference to type '" +
documentNode.getType() + "')");
} else {
throw new IllegalArgumentException("Selection for document type '" +
this.allowedType + "' can not contain references to other " +
"document types (found reference to type '" + documentNode.getType() + "')");
}
}
}
}
private void validateSelectionExpression(String sel, String allowedType) {
DocumentSelector s;
try {
s = new DocumentSelector(sel);
} catch (ParseException e) {
throw new IllegalArgumentException("Could not parse document routing selection: " + sel, e);
}
AllowedDocumentTypesChecker checker = new AllowedDocumentTypesChecker(allowedType);
s.visit(checker);
}
public String build(ModelElement elem) {
StringBuilder sb = new StringBuilder();
if (elem != null) {
for (ModelElement e : elem.subElements("document")) {
if (sb.length() > 0) {
sb.append(" OR ");
}
sb.append('(');
String type = e.getStringAttribute("type");
sb.append(type);
String selection = e.getStringAttribute("selection");
if (selection != null) {
validateSelectionExpression(selection, type);
sb.append(" AND (");
sb.append(selection);
sb.append(')');
}
sb.append(')');
}
String globalSelection = elem.getStringAttribute("selection");
if (globalSelection != null) {
validateSelectionExpression(globalSelection, null);
StringBuilder global = new StringBuilder();
global.append('(').append(globalSelection).append(") AND (")
.append(sb.toString()).append(')');
return global.toString();
}
}
return sb.toString();
}
}
|