/** * Parses the term passed in the parameter and returns a JIPTerm object that wraps the * corresponding prolog term.<br> * If the string passed in the parameter doesn't contains a valid prolog term it raises a * JIPSyntaxErrorException. * * @param strTerm Term to be parsed. If the term doesn't end with a "dot" one is appended to it. * @return a new JIPTerm object. * @exception com.ugos.jiprolog.engine.JIPSyntaxErrorException */ public final JIPTerm parseTerm(String strTerm) throws JIPSyntaxErrorException { try { final byte[] btTerm = strTerm.getBytes(getEncoding()); final ByteArrayInputStream is = new ByteArrayInputStream(btTerm); PrologParser parser = new PrologParser( new ParserReader(new PushbackLineNumberInputStream(is)), m_opManager, m_engine, "user"); m_singletonVars = parser.getSingletonVariables(); final PrologObject term = parser.parseNext(); return JIPTerm.getJIPTerm(term); } catch (UnsupportedEncodingException ex) { throw new JIPRuntimeException(ex.getMessage()); } }
private JIPTerm parseNextTerm() throws JIPSyntaxErrorException { final PrologObject term = m_parser.parseNext(); if (term != null) return JIPTerm.getJIPTerm(term); else return null; }
public static final void pack(List fileList, String destinationFile, final JIPEngine engine) throws FileNotFoundException, IOException { InputStream ins = null; File outf = new File(destinationFile); if (!outf.isAbsolute()) outf = new File(engine.getSearchPath(), destinationFile); final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(outf)); String strPath = null; int len = fileList.getHeight(); for (int i = 1; i <= len; i++) { try { PrologObject path = getRealTerm(fileList.getTerm(i)); if (!(path instanceof Atom)) throw new JIPTypeException(JIPTypeException.ATOM_OR_STRING, path); strPath = ((Atom) path).getName(); String strFileName[] = new String[1]; String strCurDir[] = new String[1]; ins = StreamManager.getStreamManager() .getInputStream(strPath, engine.getSearchPath(), strFileName, strCurDir); String file = new File(strPath).getName(); final int nPos = file.lastIndexOf('.'); file = new StringBuilder(file.substring(0, nPos)).append(".jip").toString(); PrologParser parser = new PrologParser( new ParserReader(new PushbackLineNumberInputStream(ins)), engine.getOperatorManager(), engine, strPath); try { PrologObject term; while ((term = parser.parseNext()) != null) { out.writeObject(term); } ins.close(); ins = null; } catch (IOException ex) { ins.close(); ins = null; out.close(); ex.printStackTrace(); throw new JIPJVMException(ex); } } catch (FileNotFoundException ex) { try { if (ins != null) ins.close(); out.close(); } catch (IOException ex1) { } ; throw JIPExistenceException.createSourceSynkException(Atom.createAtom(strPath)); } catch (IOException ex) { try { if (ins != null) ins.close(); } catch (IOException ex1) { } throw new JIPJVMException(ex); } catch (SecurityException ex) { try { if (ins != null) ins.close(); } catch (IOException ex1) { } throw new JIPPermissionException("access", "source_sink", Atom.createAtom(strPath)); } catch (JIPRuntimeException ex) { ex.m_strFileName = strPath; throw ex; } finally { try { if (ins != null) ins.close(); } catch (IOException ex1) { } } } out.close(); }