Project

General

Profile

1
package eu.dnetlib.index.utils;
2

    
3
import java.io.IOException;
4

    
5
import javax.xml.transform.Transformer;
6
import javax.xml.transform.TransformerConfigurationException;
7
import javax.xml.transform.TransformerException;
8
import javax.xml.transform.TransformerFactory;
9
import javax.xml.transform.TransformerFactoryConfigurationError;
10

    
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.dom4j.Document;
14
import org.dom4j.DocumentException;
15
import org.dom4j.io.DocumentResult;
16
import org.dom4j.io.DocumentSource;
17
import org.dom4j.io.SAXReader;
18
import org.springframework.beans.factory.annotation.Required;
19
import org.springframework.core.io.Resource;
20

    
21
import eu.dnetlib.rmi.provision.IndexServiceException;
22

    
23

    
24
public class IndexSchemaFactory {
25

    
26
	private static final Log log = LogFactory.getLog(IndexSchemaFactory.class); // NOPMD by marko on 11/24/08 5:02 PM
27

    
28
	private final static String TEXT_FIELD_TYPE = "textFieldType";
29

    
30
	private Resource schemaTemplate;
31

    
32
	private String textFieldType;
33

    
34
	/**
35
	 * Transformer used to get the index schema.
36
	 */
37
	private Transformer transformer;
38

    
39
	public void init() throws TransformerConfigurationException, TransformerFactoryConfigurationError, DocumentException, IOException {
40

    
41
		transformer = TransformerFactory.newInstance().newTransformer(new DocumentSource(new SAXReader().read(getSchemaTemplate().getInputStream())));
42
		transformer.setParameter(TEXT_FIELD_TYPE, getTextFieldType());
43
	}
44

    
45
	/**
46
	 * @param fields
47
	 *            input document
48
	 * @return the application of the schemaXslt transformation to the docIn document
49
	 * @throws IndexServiceException
50
	 */
51
	public String getSchema(final Document fields) throws IndexServiceException {
52

    
53
		log.debug("default field type: " + getTextFieldType());
54
		log.debug("building schema from fields: \n" + fields.asXML() + "\n");
55

    
56
		final DocumentResult result = new DocumentResult();
57
		try {
58
			transformer.transform(new DocumentSource(fields), result);
59
			String xml = result.getDocument().asXML();
60

    
61
			log.debug("new index schema:\n" + xml);
62

    
63
			return xml;
64
		} catch (TransformerException e) {
65
			throw new IndexServiceException(e);
66
		}
67
	}
68

    
69
	@Required
70
	public void setSchemaTemplate(final Resource schemaTemplate) {
71
		this.schemaTemplate = schemaTemplate;
72
	}
73

    
74
	public Resource getSchemaTemplate() {
75
		return schemaTemplate;
76
	}
77

    
78
	@Required
79
	public void setTextFieldType(final String textFieldType) {
80
		this.textFieldType = textFieldType;
81
	}
82

    
83
	public String getTextFieldType() {
84
		return textFieldType;
85
	}
86

    
87
}
(5-5/9)