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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.application;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.RegionName;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
/**
* A preprocessor for services.xml files that handles deploy:environment, deploy:region, preprocess:properties, preprocess:include
* and create a new Document which is based on the supplied environment and region
*
* @author hmusum
*/
public class XmlPreProcessor {
final static String deployNamespace = "xmlns:deploy";
final static String deployNamespaceUri = "vespa";
final static String preprocessNamespace = "xmlns:preprocess";
final static String preprocessNamespaceUri = "properties"; //TODO
private final File applicationDir;
private final Reader xmlInput;
private final InstanceName instance;
private final Environment environment;
private final RegionName region;
private final List<PreProcessor> chain;
// TODO: Remove after September 2019
public XmlPreProcessor(File applicationDir, File xmlInput, Environment environment, RegionName region) throws IOException {
this(applicationDir, new FileReader(xmlInput), InstanceName.from("default"), environment, region);
}
public XmlPreProcessor(File applicationDir, File xmlInput, InstanceName instance, Environment environment, RegionName region) throws IOException {
this(applicationDir, new FileReader(xmlInput), instance, environment, region);
}
// TODO: Remove after September 2019
public XmlPreProcessor(File applicationDir, Reader xmlInput, Environment environment, RegionName region) throws IOException {
this(applicationDir, xmlInput, InstanceName.from("default"), environment, region);
}
public XmlPreProcessor(File applicationDir, Reader xmlInput, InstanceName instance, Environment environment, RegionName region) {
this.applicationDir = applicationDir;
this.xmlInput = xmlInput;
this.instance = instance;
this.environment = environment;
this.region = region;
this.chain = setupChain();
}
public Document run() throws ParserConfigurationException, IOException, SAXException, TransformerException {
DocumentBuilder docBuilder = Xml.getPreprocessDocumentBuilder();
Document document = docBuilder.parse(new InputSource(xmlInput));
return execute(document);
}
private Document execute(Document input) throws IOException, TransformerException {
for (PreProcessor preProcessor : chain) {
input = preProcessor.process(input);
}
return input;
}
private List<PreProcessor> setupChain() {
List<PreProcessor> chain = new ArrayList<>();
chain.add(new IncludeProcessor(applicationDir));
chain.add(new OverrideProcessor(instance, environment, region));
chain.add(new PropertiesProcessor());
return chain;
}
}
|