source: ComponentRegistry/trunk/ComponentSpecFixer/src/main/java/nl/mpi/componentspecfixer/ComponentSpecFixer.java @ 6223

Last change on this file since 6223 was 6223, checked in by Twan Goosen, 9 years ago

comments and pom to make executable jar

File size: 5.0 KB
Line 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package nl.mpi.componentspecfixer;
7
8import clarin.cmdi.componentregistry.model.BaseDescription;
9import clarin.cmdi.componentregistry.persistence.ComponentDao;
10import java.io.InputStream;
11import java.io.StringReader;
12import java.io.StringWriter;
13import java.util.List;
14import javax.xml.transform.Result;
15import javax.xml.transform.Source;
16import javax.xml.transform.Transformer;
17import javax.xml.transform.TransformerConfigurationException;
18import javax.xml.transform.TransformerException;
19import javax.xml.transform.TransformerFactory;
20import javax.xml.transform.stream.StreamResult;
21import javax.xml.transform.stream.StreamSource;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24import org.springframework.context.support.ClassPathXmlApplicationContext;
25import org.springframework.transaction.PlatformTransactionManager;
26import org.springframework.transaction.TransactionStatus;
27import org.springframework.transaction.support.TransactionCallbackWithoutResult;
28import org.springframework.transaction.support.TransactionTemplate;
29
30/**
31 * This utility will apply a style sheet to all component specifications in the
32 * Component Registry database (versions 1.14.5 and higher) that minimizes all
33 * specifications that have consolidated expansion (components with both a
34 * ComponentId attribute and children).
35 *
36 *
37 * Usage:
38 * - make sure no other application is connected to the database
39 * - run the main
40 * @author Twan Goosen <twan.goosen@mpi.nl>
41 */
42public class ComponentSpecFixer {
43
44    private static final Logger logger = LoggerFactory.getLogger(ComponentSpecFixer.class);
45    private static final String XSLT_RESOURCE = "/xslt/collapse-component-spec.xsl";
46    private static final boolean DRY_RUN = false;
47
48    private ClassPathXmlApplicationContext applicationContext;
49    private ComponentDao componentDao;
50    private TransactionTemplate transactionTemplate;
51    private Transformer transformer;
52
53    private void init() throws TransformerConfigurationException {
54        applicationContext = new ClassPathXmlApplicationContext("/spring-config/applicationContext.xml", "/spring-config/container-environment.xml");
55        componentDao = applicationContext.getBean(ComponentDao.class);
56        transactionTemplate = new TransactionTemplate(applicationContext.getBean(PlatformTransactionManager.class));
57
58        // create transformer
59        final TransformerFactory factory = TransformerFactory.newInstance();
60        final InputStream xsltStream = getClass().getResourceAsStream(XSLT_RESOURCE);
61        transformer = factory.newTransformer(new StreamSource(xsltStream));
62    }
63
64    private void run() {
65        final List<BaseDescription> descriptions = componentDao.getAllNonDeletedDescriptions();
66        logger.info("Found {} components", descriptions.size());
67
68        fixSpecs(descriptions);
69    }
70
71    private void fixSpecs(List<BaseDescription> descriptions) {
72        for (final BaseDescription descr : descriptions) {
73            final String id = descr.getId();
74
75            // read and update in a transaction
76            transactionTemplate.execute(new TransactionCallbackWithoutResult() {
77
78                @Override
79                protected void doInTransactionWithoutResult(TransactionStatus ts) {
80                    logger.debug("Updating {}", id);
81
82                    final String content = componentDao.getContent(false, id);
83                    try {
84                        final String newContent = fixXml(content);
85                        if (content.length() / newContent.length() > 1) {
86                            logger.info("Reduced {} from {} to {} characters", id, content.length(), newContent.length());
87
88                            if (!DRY_RUN) {
89                                componentDao.updateDescription(descr.getDbId(), descr, newContent);
90                            }
91                        } else {
92                            logger.info("No significant reduction for {}", id);
93                        }
94
95                    } catch (TransformerException ex) {
96                        throw new RuntimeException("Failed to transform " + id, ex);
97                    }
98                }
99            });
100
101        }
102    }
103
104    private String fixXml(String content) throws TransformerException {
105        final StringReader sourceReader = new StringReader(content);
106        final Source source = new StreamSource(sourceReader);
107        final StringWriter resultWriter = new StringWriter();
108        final Result result = new StreamResult(resultWriter);
109        transformer.transform(source, result);
110        return resultWriter.toString();
111    }
112
113    public static void main(String[] args) throws TransformerConfigurationException {
114        final ComponentSpecFixer componentSpecFixer = new ComponentSpecFixer();
115
116        logger.info("Initializing...");
117        componentSpecFixer.init();
118
119        logger.info("Running fixer...");
120        componentSpecFixer.run();
121    }
122}
Note: See TracBrowser for help on using the repository browser.