Changeset 6222 for ComponentRegistry
- Timestamp:
- 05/19/15 08:02:58 (9 years ago)
- 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 8 8 import clarin.cmdi.componentregistry.model.BaseDescription; 9 9 import clarin.cmdi.componentregistry.persistence.ComponentDao; 10 import java.io.InputStream; 11 import java.io.StringReader; 12 import java.io.StringWriter; 10 13 import java.util.List; 14 import javax.xml.transform.Result; 15 import javax.xml.transform.Source; 16 import javax.xml.transform.Transformer; 17 import javax.xml.transform.TransformerConfigurationException; 18 import javax.xml.transform.TransformerException; 19 import javax.xml.transform.TransformerFactory; 20 import javax.xml.transform.stream.StreamResult; 21 import javax.xml.transform.stream.StreamSource; 11 22 import org.slf4j.Logger; 12 23 import org.slf4j.LoggerFactory; … … 14 25 import org.springframework.transaction.PlatformTransactionManager; 15 26 import org.springframework.transaction.TransactionStatus; 16 import org.springframework.transaction.support.TransactionCallback ;27 import org.springframework.transaction.support.TransactionCallbackWithoutResult; 17 28 import org.springframework.transaction.support.TransactionTemplate; 18 29 … … 23 34 public class ComponentSpecFixer { 24 35 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; 26 39 27 40 private ClassPathXmlApplicationContext applicationContext; 28 41 private ComponentDao componentDao; 29 42 private TransactionTemplate transactionTemplate; 43 private Transformer transformer; 30 44 31 private void init() {45 private void init() throws TransformerConfigurationException { 32 46 applicationContext = new ClassPathXmlApplicationContext("/spring-config/applicationContext.xml", "/spring-config/container-environment.xml"); 33 47 componentDao = applicationContext.getBean(ComponentDao.class); 34 48 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)); 35 54 } 36 55 … … 46 65 final String id = descr.getId(); 47 66 48 transactionTemplate.execute(new TransactionCallback () {67 transactionTemplate.execute(new TransactionCallbackWithoutResult() { 49 68 50 69 @Override 51 p ublic Object doInTransaction(TransactionStatus ts) {52 logger. info("Updating {}", id);70 protected void doInTransactionWithoutResult(TransactionStatus ts) { 71 logger.debug("Updating {}", id); 53 72 54 73 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()); 58 78 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 } 60 89 } 61 90 }); … … 64 93 } 65 94 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 { 67 105 final ComponentSpecFixer componentSpecFixer = new ComponentSpecFixer(); 68 106
Note: See TracChangeset
for help on using the changeset viewer.