package org.autoplot.idlsupport; import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.autoplot.datasource.AbstractDataSourceFactory; import org.autoplot.datasource.CompletionContext; import org.autoplot.datasource.DataSetURI; import org.autoplot.datasource.DataSource; import org.autoplot.datasource.URISplit; import org.autoplot.idlsupport.ReadIDLSav.ArrayDesc; import org.autoplot.idlsupport.ReadIDLSav.StructDesc; import org.autoplot.idlsupport.ReadIDLSav.TagDesc; import org.das2.util.monitor.ProgressMonitor; /** * Factory for reading IDLSave files. * @author jbf */ public class IdlsavDataSourceFactory extends AbstractDataSourceFactory { @Override public DataSource getDataSource(URI uri) throws Exception { return new IdlsavDataSource(uri); } @Override public boolean reject(String surl, List problems, ProgressMonitor mon) { try { URISplit split= URISplit.parse(surl); Map params= URISplit.parseParams(split.params); String var= params.get(URISplit.PARAM_ARG_0); if ( var==null ) { problems.add("need variable name to read"); return true; } List ccresult= new ArrayList<>(); File file= DataSetURI.getFile( split.resourceUri, mon ); ByteBuffer buf= ReadIDLSav.readFileIntoByteBuffer(file); String[] names= new ReadIDLSav().readVarNames(buf); String[] vars= var.split(",",-2); for ( int ivar=0; ivar0 ; } catch (IOException ex) { problems.add( ex.toString() ); return true; } } private void addCompletions( ReadIDLSav reader, String root, String key, ByteBuffer buf, List ccresult ) throws IOException { String keyn= root==null ? key : root + "." + key; if ( root!=null ) { Object o= reader.readVar( buf, root ); Map m= (Map)o; for ( Entry e: m.entrySet() ) { CompletionContext cc1= new CompletionContext( CompletionContext.CONTEXT_PARAMETER_NAME, keyn, this, "arg_0", root + "." + key, "", true ); ccresult.add(cc1); } } if ( reader.isStructure( buf, key ) ) { StructDesc desc= (StructDesc)reader.readTagDesc( buf, key ); for ( String t : desc.tagnames ) { CompletionContext cc1= new CompletionContext( CompletionContext.CONTEXT_PARAMETER_NAME, keyn+"." +t, this, "arg_0", keyn+"." +t, "", true ); ccresult.add(cc1); } } else if ( reader.isArray( buf, key ) ) { ArrayDesc desc= (ArrayDesc)reader.readTagDesc( buf, key ); StringBuilder sqube= new StringBuilder("[").append(String.valueOf(desc.dims[0])); for ( int i=1; i getCompletions(CompletionContext cc, ProgressMonitor mon) throws Exception { if ( cc.context.equals(CompletionContext.CONTEXT_PARAMETER_NAME) ) { List ccresult= new ArrayList<>(); File file= DataSetURI.getFile( cc.resourceURI, mon ); String completable= cc.params; ByteBuffer buf= ReadIDLSav.readFileIntoByteBuffer(file); String[] names= new ReadIDLSav().readVarNames(buf); ReadIDLSav reader= new ReadIDLSav(); if ( completable.contains(".") ) { int i= completable.lastIndexOf('.'); String root= completable.substring(0,i); Object o= reader.readVar( buf, root ); Map m= (Map)o; for ( Entry e: m.entrySet() ) { if ( e.getValue() instanceof Map ) { for ( Entry e2: ((Map)e.getValue()).entrySet() ) { CompletionContext cc1= new CompletionContext( CompletionContext.CONTEXT_PARAMETER_NAME, root + "." + e.getKey() + "."+ e2.getKey(), this, "arg_0", root + "." + e.getKey()+ "."+ e2.getKey(), "", true ); ccresult.add(cc1); } } else { CompletionContext cc1= new CompletionContext( CompletionContext.CONTEXT_PARAMETER_NAME, root + "." + e.getKey(), this, "arg_0", root + "." + e.getKey(), "", true ); ccresult.add(cc1); } } } else { for ( int i=0; i