Changeset 6222 for ComponentRegistry


Ignore:
Timestamp:
05/19/15 08:02:58 (9 years ago)
Author:
Twan Goosen
Message:

now applying stylesheet and updating DB

Location:
ComponentRegistry/trunk/ComponentSpecFixer/src/main
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentSpecFixer/src/main/java/nl/mpi/componentspecfixer/ComponentSpecFixer.java

    r6221 r6222  
    88import clarin.cmdi.componentregistry.model.BaseDescription;
    99import clarin.cmdi.componentregistry.persistence.ComponentDao;
     10import java.io.InputStream;
     11import java.io.StringReader;
     12import java.io.StringWriter;
    1013import 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;
    1122import org.slf4j.Logger;
    1223import org.slf4j.LoggerFactory;
     
    1425import org.springframework.transaction.PlatformTransactionManager;
    1526import org.springframework.transaction.TransactionStatus;
    16 import org.springframework.transaction.support.TransactionCallback;
     27import org.springframework.transaction.support.TransactionCallbackWithoutResult;
    1728import org.springframework.transaction.support.TransactionTemplate;
    1829
     
    2334public class ComponentSpecFixer {
    2435
    25     private final static Logger logger = LoggerFactory.getLogger(ComponentSpecFixer.class);
     36    private static final Logger logger = LoggerFactory.getLogger(ComponentSpecFixer.class);
     37    private static final String XSLT_RESOURCE = "/xslt/collapse-component-spec.xsl";
     38    private static final boolean DRY_RUN = false;
    2639
    2740    private ClassPathXmlApplicationContext applicationContext;
    2841    private ComponentDao componentDao;
    2942    private TransactionTemplate transactionTemplate;
     43    private Transformer transformer;
    3044
    31     private void init() {
     45    private void init() throws TransformerConfigurationException {
    3246        applicationContext = new ClassPathXmlApplicationContext("/spring-config/applicationContext.xml", "/spring-config/container-environment.xml");
    3347        componentDao = applicationContext.getBean(ComponentDao.class);
    3448        transactionTemplate = new TransactionTemplate(applicationContext.getBean(PlatformTransactionManager.class));
     49
     50        // create transformer
     51        final TransformerFactory factory = TransformerFactory.newInstance();
     52        final InputStream xsltStream = getClass().getResourceAsStream(XSLT_RESOURCE);
     53        transformer = factory.newTransformer(new StreamSource(xsltStream));
    3554    }
    3655
     
    4665            final String id = descr.getId();
    4766
    48             transactionTemplate.execute(new TransactionCallback() {
     67            transactionTemplate.execute(new TransactionCallbackWithoutResult() {
    4968
    5069                @Override
    51                 public Object doInTransaction(TransactionStatus ts) {
    52                     logger.info("Updating {}", id);
     70                protected void doInTransactionWithoutResult(TransactionStatus ts) {
     71                    logger.debug("Updating {}", id);
    5372
    5473                    final String content = componentDao.getContent(false, id);
    55                     //TODO: update content
    56                     final String newContent = content;
    57                     componentDao.updateDescription(descr.getDbId(), descr, newContent);
     74                    try {
     75                        final String newContent = fixXml(content);
     76                        if (content.length() / newContent.length() > 1) {
     77                            logger.info("Reduced {} from {} to {} characters", id, content.length(), newContent.length());
    5878
    59                     return null;
     79                            if (!DRY_RUN) {
     80                                componentDao.updateDescription(descr.getDbId(), descr, newContent);
     81                            }
     82                        } else {
     83                            logger.info("No significant reduction for {}", id);
     84                        }
     85
     86                    } catch (TransformerException ex) {
     87                        throw new RuntimeException("Failed to transform " + id, ex);
     88                    }
    6089                }
    6190            });
     
    6493    }
    6594
    66     public static void main(String[] args) {
     95    private String fixXml(String content) throws TransformerException {
     96        final StringReader sourceReader = new StringReader(content);
     97        final Source source = new StreamSource(sourceReader);
     98        final StringWriter resultWriter = new StringWriter();
     99        final Result result = new StreamResult(resultWriter);
     100        transformer.transform(source, result);
     101        return resultWriter.toString();
     102    }
     103
     104    public static void main(String[] args) throws TransformerConfigurationException {
    67105        final ComponentSpecFixer componentSpecFixer = new ComponentSpecFixer();
    68106
Note: See TracChangeset for help on using the changeset viewer.