/*******************************************************************************
* Copyright (c) 2009-2013 CWI
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* * Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI
* * Mark Hills - Mark.Hills@cwi.nl (CWI)
* * Arnold Lankamp - Arnold.Lankamp@cwi.nl
*******************************************************************************/
package org.rascalmpl.library.lang.aterm;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import org.eclipse.imp.pdb.facts.IConstructor;
import org.eclipse.imp.pdb.facts.ISourceLocation;
import org.eclipse.imp.pdb.facts.IString;
import org.eclipse.imp.pdb.facts.IValue;
import org.eclipse.imp.pdb.facts.IValueFactory;
import org.eclipse.imp.pdb.facts.exceptions.FactTypeUseException;
import org.eclipse.imp.pdb.facts.io.ATermReader;
import org.eclipse.imp.pdb.facts.io.ATermWriter;
import org.eclipse.imp.pdb.facts.type.Type;
import org.eclipse.imp.pdb.facts.type.TypeStore;
import org.rascalmpl.interpreter.IEvaluatorContext;
import org.rascalmpl.interpreter.TypeReifier;
import org.rascalmpl.interpreter.utils.RuntimeExceptionFactory;
public class IO{
private final IValueFactory values;
public IO(IValueFactory values){
super();
this.values = values;
}
public IValue readTextATermFile(IValue type, ISourceLocation loc, IEvaluatorContext ctx){
TypeStore store = new TypeStore();
Type start = new TypeReifier(ctx.getValueFactory()).valueToType((IConstructor) type, store);
InputStream in = null;
try{
in = ctx.getResolverRegistry().getInputStream(loc.getURI());
return new ATermReader().read(values, store, start, in);
}catch(IOException e){
throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
}finally{
if(in != null){
try{
in.close();
}catch(IOException ioex){
throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
}
}
}
}
public IValue readATermFromFile(IString fileName){
//@doc{readATermFromFile -- read an ATerm from a named file}
ATermReader atr = new ATermReader();
try {
FileInputStream stream = new FileInputStream(fileName.getValue());
IValue result = atr.read(values, stream);
stream.close();
return result;
} catch (FactTypeUseException e) {
e.printStackTrace();
throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
} catch (IOException e) {
e.printStackTrace();
throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
}
}
public void writeTextATermFile(ISourceLocation loc, IValue value, IEvaluatorContext ctx){
OutputStream out = null;
try{
out = ctx.getResolverRegistry().getOutputStream(loc.getURI(), false);
new ATermWriter().write(value, new OutputStreamWriter(out, "UTF8"));
}catch(IOException e){
throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
}finally{
if(out != null){
try{
out.close();
}catch(IOException ioex){
throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
}
}
}
}
}