Project

General

Profile

1
package eu.dnetlib.functionality.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.data.provision.index.rmi.IndexServiceException;
22

    
23
public class IndexSchemaFactory {
24

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

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

    
29
	private Resource schemaTemplate;
30

    
31
	private String textFieldType;
32

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

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

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

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

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

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

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

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

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

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

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

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

    
86
}
(2-2/4)