/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.autoplot.cefdatasource; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.das2.util.monitor.ProgressMonitor; import org.autoplot.cefdatasource.CefReaderHeader.ParamStruct; import org.autoplot.datasource.AbstractDataSourceFactory; import org.autoplot.datasource.CompletionContext; import org.autoplot.datasource.DataSetURI; import org.autoplot.datasource.DataSource; /** * * @author jbf */ public class CefDataSourceFactory extends AbstractDataSourceFactory { @Override public DataSource getDataSource(URI uri) throws Exception { return new CefDataSource(uri); } private List getPlottable(URI uri, ProgressMonitor mon ) throws IOException { File f = DataSetURI.getFile(uri, mon ); ReadableByteChannel in = Channels.newChannel(new FileInputStream(f)); CefReaderHeader reader = new CefReaderHeader(); Cef cef = reader.read(in); Map h = cef.parameters; List result = new ArrayList(); for ( Entry ee: h.entrySet() ) { ParamStruct h1= ee.getValue(); String parameterType= (String)h1.entries.get("PARAMETER_TYPE"); if ( parameterType!=null && parameterType.equals("Data") ) { result.add(ee.getKey()); } } for ( Entry ee: h.entrySet() ) { ParamStruct h1= ee.getValue(); String parameterType= (String)h1.entries.get("PARAMETER_TYPE"); if ( parameterType==null || !parameterType.equals("Data") ) { result.add(ee.getKey()); } } return result; } @Override public List getCompletions(CompletionContext cc,org.das2.util.monitor.ProgressMonitor mon) { List result = new ArrayList(); if (cc.context == CompletionContext.CONTEXT_PARAMETER_NAME) { try { String surl = CompletionContext.get(CompletionContext.CONTEXT_FILE, cc); List plottable = getPlottable( DataSetURI.getResourceURI(surl), mon ); for (String s : plottable) { result.add(new CompletionContext( CompletionContext.CONTEXT_PARAMETER_NAME, s, this, "arg_0" ,null ,null, true )); } } catch (MalformedURLException ex) { throw new RuntimeException(ex); } catch (IOException ex) { throw new RuntimeException(ex); } } return result; } @Override public boolean reject( String surl, List problems,ProgressMonitor mon ) { return ! surl.contains("?") || surl.indexOf("?")==surl.length()-1; } }