ExperimentFileParser.java000075500000000000000000000312121152651277300115400package edu.cmu.search.bridges; import java.util.HashMap; import java.io.StringReader; import org.xml.sax.InputSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.Reader; import java.util.Iterator; import java.util.NoSuchElementException; import ome.services.messages.RegisterServiceCleanupMessage; import ome.system.OmeroContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class ExperimentFileParser implements ApplicationContextAware { private final static Log log = LogFactory.getLog(ExperimentFileParser.class); protected OmeroContext context; public void setApplicationContext(ApplicationContext arg0) throws BeansException { context = (OmeroContext) arg0; } /** * {@link Iterable} which returns an empty {@link Iterator}. This will be * used in case */ public final static Iterable EMPTY = new Iterable() { public Iterator iterator() { return new Iterator() { public boolean hasNext() { return false; } public Reader next() { throw new NoSuchElementException(); } public void remove() { throw new UnsupportedOperationException(); } }; } }; final public HashMap parse(File file) { if (file == null) { log.warn("Argument null. Returning EMPTY:"); return null; } if (!file.exists() && !file.canRead()) { log.debug("empty|unreadable file: " + file.getAbsoluteFile()); return null; } try { String it = doParse(file); if (it == null) { log.debug("Implementation returned null."); return null; } else { log.debug("Implementation returned Something."); HashMap map = null; map = parseXML(it); return map; } } catch (Exception e) { log.warn("Implementation threw an exception.", e); return null; } } /** * Template method to parse a {@link File} into manageable chunks. * * The default implementation reads from the file lazily with chunks * overlapping on the final white space. For example a file with: * The quick brown fox jumps over the lazy dog might be * parsed to: The quick brown fox jumps and * jumps over the lazy dog. * * Receives a non-null, {@link File#canRead() readable} {@link File} * instance from {@link #parse(File)} and can return a possible null * {@link Iterable} or throw an {@link Exception}. * * In any of the non-successful cases, the {@link #EMPTY} {@link Iterable} * will be returned to the consumer. */ public String doParse(File file) throws Exception { String XML = readFile(file); // FileReader reader = new FileReader(file); // BufferedReader buffered = new BufferedReader(reader); // context.publishEvent(new RegisterServiceCleanupMessage(this, buffered) { // @Override // public void close() { // try { // Reader r = (Reader) resource; // r.close(); // } catch (Exception e) { //// log.debug("Error closing " + resource, e); // } // } // }); // Iterator it = new SingleIterator(buffered); // return wrap(it); return XML; } public String readFile(File file) throws Exception { FileReader reader = new FileReader(file); BufferedReader buffered = new BufferedReader(reader); StringBuilder contents = new StringBuilder(); try { String line = null; //not declared within while loop /* * readLine is a bit quirky : * it returns the content of a line MINUS the newline. * it returns null only for the END of the stream. * it returns an empty String if two newlines appear in a row. */ while (( line = buffered.readLine()) != null){ contents.append(line); contents.append(System.getProperty("line.separator")); } } finally { buffered.close(); } return contents.toString(); } /** * Wraps an {@link Iterator} with an {@link Iterable} instance. If the * {@link Iterator} is null, the {@link #EMPTY} {@link Iterable} will be * returned. * * @param it * Can be null. * @return Will never be null */ public Iterable wrap(Iterator it) { if (it == null) { return EMPTY; } return new IteratorWrapper(it); } public Iterable wrap(Reader r) { if (r == null) { return EMPTY; } return wrap(new SingleIterator(r)); } private static class SingleIterator implements Iterator { Reader r; SingleIterator(Reader r) { this.r = r; } public boolean hasNext() { return r != null; } public Reader next() { Reader rv = r; r = null; return rv; } public void remove() { throw new UnsupportedOperationException(); } } private static class IteratorWrapper implements Iterable { private final Iterator it; public IteratorWrapper(Iterator it) { this.it = it; } public Iterator iterator() { return it; } } private static class OverlappingChunkFileIterator implements Iterator { private static final String linesep = System .getProperty("line.separator"); private static final int size = 10000; private final long fileSize; private final char[] buf; private String next; /* * will be closed nulled out when finished. */ private BufferedReader reader; public OverlappingChunkFileIterator(File file) throws Exception { this.fileSize = file.length(); if (fileSize > Integer.MAX_VALUE) { throw new RuntimeException(String.format( "%s file is too large for current implementation: %s", file, fileSize)); } this.reader = new BufferedReader(new FileReader(file), size); this.buf = new char[size]; } public boolean hasNext() { if (next == null) { next = doRead(); } return next != null; } public String next() { if (!hasNext()) { // does doRead() throw new NoSuchElementException(); } String rv = next; next = null; return rv; } public void remove() { throw new UnsupportedOperationException(); } /** * Intermediate method which parses whole file into a single String. * Please see the restriction in the constructor on filesize. */ private String doRead() { if (reader == null) { return null; } StringBuffer sb = new StringBuffer((int) fileSize); int rv = -1; try { while ((rv = reader.read(buf)) != -1) { sb.append(buf, 0, rv); } } catch (Exception e) { throw new RuntimeException("Error while parsing file", e); } closeReader(); return sb.toString(); } private void closeReader() { if (reader != null) { try { reader.close(); } catch (Exception e) { // must ignore } finally { reader = null; } } } } public static void loadFile(String path) throws Exception { FileReader reader = new FileReader(path); BufferedReader buffered = new BufferedReader(reader); File loadfile = new File(path); } public static HashMap parseXML(String XML) throws Exception { HashMap map = new HashMap (); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource( new StringReader( XML ) ); Document doc = db.parse( is ); //this parses the XML string, if there is an error it will complain here doc.normalizeDocument(); String basename = doc.getElementsByTagName("step").item(0).getFirstChild().getNextSibling().getTextContent(); //System.out.println( basename ); NodeList listOfParameters = doc.getElementsByTagName("parameter"); for( int i=0; i map = null; // First you need to get the annotations and look for a FileAnnotation for (Annotation a : i.linkedAnnotationList()) { if (a instanceof FileAnnotation) { FileAnnotation ann = (FileAnnotation) a; tagNs = ann.getNs(); tagName = ann.getFile().getName(); subtagName = tagName.substring(tagName.length()-7); if (tagNs.equalsIgnoreCase("openmicroscopy.org/omero/editor/experiment") && subtagName.equalsIgnoreCase("cpe.xml")){ OriginalFile file = ann.getFile(); // FileParser parser = null; ome.io.nio.OriginalFilesService fileservice = (ome.io.nio.OriginalFilesService)ome.system.OmeroContext.getManagedServerContext().getBean("/OMERO/Files"); String path = fileservice.getFilesPath(file.getId()); String format = file.getMimetype(); // String XML = exParser.readFile(path); map = exParser.parse(new File(path)); } } } // parse the XML string Iterator iterator = map.keySet().iterator(); while (iterator.hasNext()) { String key = (String) iterator.next(); add(document, key, map.get(key), _opts); // System.out.print("key="+key); // System.out.println(" vale="+map.get(key)); } // // final List list = new ArrayList(); // // for (final DatasetImageLink dil : i.unmodifiableDatasetLinks()) { // final Dataset d = dil.parent(); // for (final ProjectDatasetLink pdl : d.unmodifiableProjectLinks()) { // list.add(pdl.parent()); // } // } // if (list.size() > 0) { // // ticket:955 Disabling for the moment. // // reindexAll(list); // } } else if (value instanceof OriginalFile) { // 1. to recognize if it's the experiment file. If it's not, do nothing from here. // 2. to parse the XML file // 3. map all the parsed values to Lucene Index } } } /* public static void downloadFile(RawFileStorePrx rawfilestore, OriginalFile originalfile, File file) throws Exception { int INC = 262144; Long fileId = originalfile.getId().getValue(); rawfilestore.setFileId(fileId); Long fileSize = originalfile.getSize().getValue(); String path = file.getAbsolutePath(); int offset = 0; int length = fileSize.intValue();//Convert.ToInt32(fileSize);//(int)fileSize; try { FileOutputStream stream = new FileOutputStream(file); try { try { for (offset = 0; (offset+INC) < fileSize;) { stream.write(rawfilestore.read(offset, INC)); offset += INC; } } finally { stream.write(rawfilestore.read(offset, length-offset)); stream.close(); } } catch (Exception e) { if (stream != null) stream.close(); if (file != null) file.delete(); } } catch (IOException e) { if (file != null) file.delete(); //closeService(rawfilestore); //store.close() rawfilestore.close(); // throw new DSAccessException("Cannot create file " +path, e); } rawfilestore.close();//closeService(store); // store.close() throws an exception } */ /* if (value instanceof Project) { logger().info("Indexing all image names for " + value); // Copying lucene options with a new boost value final float reduced_boost = _opts.getBoost().floatValue() / 2; final LuceneOptions opts = new SimpleLuceneOptions(_opts, reduced_boost); final Project p = (Project) value; for (final ProjectDatasetLink pdl : p.unmodifiableDatasetLinks()) { final Dataset d = pdl.child(); for (final DatasetImageLink dil : d.unmodifiableImageLinks()) { final Image i = dil.child(); // Name is never null, but as an example it is important // to always check the value for null, and either simply // not call add() or to use a null token like "null". if (i.getName() != null) { add(document, "image_name", i.getName(), opts); } else { add(document, "image_name", "null", opts); } } } } */