/** * Creates a new {@link SnuggleSession} using the default {@link SessionConfiguration} * if se */ public SnuggleSession createSession() { SessionConfiguration sessionConfiguration = defaultSessionConfiguration; if (sessionConfiguration!=null) { sessionConfiguration = new SessionConfiguration(); } return createSession(sessionConfiguration); }
/** * Creates a {@link SnuggleSnapshot} Object holding the current state of this session that can * be later used to recreate a session having exactly the same state. */ public SnuggleSnapshot createSnapshot() { return new SnuggleSnapshot(engine, (SessionConfiguration) configuration.clone(), new ArrayList<InputError>(errors), new HashMap<String, UserDefinedCommand>(userCommandMap), new HashMap<String, UserDefinedEnvironment>(userEnvironmentMap), new ArrayList<FlowToken>(parsedTokens)); }
/** * Records a new error, throwing a {@link SnuggleParseException} if * the current {@link SessionConfiguration} deems that we should * fail on the first error. * * @param inputError {@link InputError} to register, which must not be null. * * @throws SnuggleParseException if this session is configured to fail fast */ public void registerError(InputError inputError) throws SnuggleParseException { ConstraintUtilities.ensureNotNull(inputError, "InputError"); errors.add(inputError); if (configuration.isFailingFast()) { throw new SnuggleParseException(inputError); } } }
/** * Makes a substitution within the working document, replacing the position of the * {@link WorkingDocument} from startIndex to endIndex with the given replacement * {@link CharSequence}. * <p> * The value of {@link #position} is updated to point to the start of the replacement * afterwards. * <p> * The given {@link SourceContext} provides optional contextual information about where * the substitution came from, if required. */ private ErrorToken makeSubstitutionAndRewind(final int startIndex, final int endIndex, final CharSequence replacement) throws SnuggleParseException { int expansionLimit = sessionContext.getConfiguration().getExpansionLimit(); if (expansionLimit>0 && workingDocument.getSubstitutionDepth(startIndex) >= expansionLimit) { /* Fail: Substitution limit exceeded (avoids infinite recursion) */ return createError(CoreErrorCode.TTEU00, startIndex, endIndex, Integer.valueOf(expansionLimit)); } workingDocument.substitute(startIndex, endIndex, replacement); position = startIndex; return null; }
public LaTeXTokeniser(final SessionContext sessionContext) { this.sessionContext = sessionContext; this.modeStack = new ArrayListStack<ModeState>(); this.openEnvironmentStack = new ArrayListStack<String>(); /* Use specified NumberMatcher, or default if nothing is specified */ NumberMatcher ourNumberMatcher = sessionContext.getConfiguration().getNumberMatcher(); if (ourNumberMatcher==null) { ourNumberMatcher = new SimpleNumberMatcher(); } this.numberMatcher = ourNumberMatcher; }
/** * Convert a tex math formula into a Symja expression. The SnuggleTeX engine first converts the TeX expression in a * MathML expression. This MathML expression is then converted to Symja. * * @param texStr * the tex math formula * @return */ public IExpr toExpression(String texStr) { SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); session.getConfiguration().setFailingFast(true); SnuggleInput input = new SnuggleInput("$$ " + texStr + " $$"); try { if (session.parseInput(input)) { NodeList nodes = session.buildDOMSubtree(); int[] position = new int[] { 0 }; return convert(nodes, position, null, 0); } List<InputError> errors = session.getErrors(); for (int i = 0; i < errors.size(); i++) { fEngine.printMessage(errors.get(i).toString()); } } catch (Exception e) { if (Config.SHOW_STACKTRACE) { e.printStackTrace(); } } return F.$Aborted; }
/** * Makes a substitution within the working document, replacing the position of the * {@link WorkingDocument} from startIndex to endIndex with the given replacement * {@link CharSequence}. * <p> * The value of {@link #position} is updated to point to the start of the replacement * afterwards. * <p> * The given {@link SourceContext} provides optional contextual information about where * the substitution came from, if required. */ private ErrorToken makeSubstitutionAndRewind(final int startIndex, final int endIndex, final CharSequence replacement) throws SnuggleParseException { int expansionLimit = sessionContext.getConfiguration().getExpansionLimit(); if (expansionLimit>0 && workingDocument.getSubstitutionDepth(startIndex) >= expansionLimit) { /* Fail: Substitution limit exceeded (avoids infinite recursion) */ return createError(CoreErrorCode.TTEU00, startIndex, endIndex, Integer.valueOf(expansionLimit)); } workingDocument.substitute(startIndex, endIndex, replacement); position = startIndex; return null; }
/** * Creates a {@link SnuggleSnapshot} Object holding the current state of this session that can * be later used to recreate a session having exactly the same state. */ public SnuggleSnapshot createSnapshot() { return new SnuggleSnapshot(engine, (SessionConfiguration) configuration.clone(), new ArrayList<InputError>(errors), new HashMap<String, UserDefinedCommand>(userCommandMap), new HashMap<String, UserDefinedEnvironment>(userEnvironmentMap), new ArrayList<FlowToken>(parsedTokens)); }
/** * Creates a new {@link SnuggleSession} using the default {@link SessionConfiguration} * if se */ public SnuggleSession createSession() { SessionConfiguration sessionConfiguration = defaultSessionConfiguration; if (sessionConfiguration!=null) { sessionConfiguration = new SessionConfiguration(); } return createSession(sessionConfiguration); }
/** * Records a new error, throwing a {@link SnuggleParseException} if * the current {@link SessionConfiguration} deems that we should * fail on the first error. * * @param inputError {@link InputError} to register, which must not be null. * * @throws SnuggleParseException if this session is configured to fail fast */ public void registerError(InputError inputError) throws SnuggleParseException { ConstraintUtilities.ensureNotNull(inputError, "InputError"); errors.add(inputError); if (configuration.isFailingFast()) { throw new SnuggleParseException(inputError); } } }
/** * Makes a substitution within the working document, replacing the position of the * {@link WorkingDocument} from startIndex to endIndex with the given replacement * {@link CharSequence}. * <p> * The value of {@link #position} is updated to point to the start of the replacement * afterwards. * <p> * The given {@link SourceContext} provides optional contextual information about where * the substitution came from, if required. */ private ErrorToken makeSubstitutionAndRewind(final int startIndex, final int endIndex, final CharSequence replacement) throws SnuggleParseException { int expansionLimit = sessionContext.getConfiguration().getExpansionLimit(); if (expansionLimit>0 && workingDocument.getSubstitutionDepth(startIndex) >= expansionLimit) { /* Fail: Substitution limit exceeded (avoids infinite recursion) */ return createError(CoreErrorCode.TTEU00, startIndex, endIndex, Integer.valueOf(expansionLimit)); } workingDocument.substitute(startIndex, endIndex, replacement); position = startIndex; return null; }
/** * (This package-private constructor is used when creating a session from an existing * {@link SnuggleSnapshot} via {@link SnuggleSnapshot#createSession()}.) */ SnuggleSession(final SnuggleSnapshot snapshot) { /* Set up main worker Objects */ this.tokeniser = new LaTeXTokeniser(this); this.tokenFixer = new TokenFixer(this); /* Copy stuff from the template */ this.engine = snapshot.engine; this.configuration = (SessionConfiguration) snapshot.configuration.clone(); this.errors = new ArrayList<InputError>(snapshot.errors); this.userCommandMap = new HashMap<String, UserDefinedCommand>(snapshot.userCommandMap); this.userEnvironmentMap = new HashMap<String, UserDefinedEnvironment>(snapshot.userEnvironmentMap); this.parsedTokens = new ArrayList<FlowToken>(snapshot.parsedTokens); }
/** * Creates a new {@link SnuggleEngine} using the given {@link StylesheetManager} * for managing stylesheets. Use this if you want to integrate XSLT caching * with your own code, want more control over how things get cached or want to control * the selection of XSLT implementations. */ public SnuggleEngine(StylesheetManager stylesheetManager) { this.packages = Collections.synchronizedList(new ArrayList<SnugglePackage>()); this.defaultSessionConfiguration = new SessionConfiguration(); this.defaultDOMOutputOptions = new DOMOutputOptions(); this.defaultXMLStringOutputOptions = new XMLStringOutputOptions(); /* Create manager for XSLT stylesheets using the given cache */ this.stylesheetManager = stylesheetManager; /* Add in core package */ packages.add(CorePackageDefinitions.getPackage()); }
/** * Records a new error, throwing a {@link SnuggleParseException} if * the current {@link SessionConfiguration} deems that we should * fail on the first error. * * @param inputError {@link InputError} to register, which must not be null. * * @throws SnuggleParseException if this session is configured to fail fast */ public void registerError(InputError inputError) throws SnuggleParseException { ConstraintUtilities.ensureNotNull(inputError, "InputError"); errors.add(inputError); if (configuration.isFailingFast()) { throw new SnuggleParseException(inputError); } } }
/** * (This package-private constructor is used when creating a session from an existing * {@link SnuggleSnapshot} via {@link SnuggleSnapshot#createSession()}.) */ SnuggleSession(final SnuggleSnapshot snapshot) { /* Set up main worker Objects */ this.tokeniser = new LaTeXTokeniser(this); this.tokenFixer = new TokenFixer(this); /* Copy stuff from the template */ this.engine = snapshot.engine; this.configuration = (SessionConfiguration) snapshot.configuration.clone(); this.errors = new ArrayList<InputError>(snapshot.errors); this.userCommandMap = new HashMap<String, UserDefinedCommand>(snapshot.userCommandMap); this.userEnvironmentMap = new HashMap<String, UserDefinedEnvironment>(snapshot.userEnvironmentMap); this.parsedTokens = new ArrayList<FlowToken>(snapshot.parsedTokens); }
/** * Creates a new {@link SnuggleEngine} using the given {@link StylesheetManager} * for managing stylesheets. Use this if you want to integrate XSLT caching * with your own code, want more control over how things get cached or want to control * the selection of XSLT implementations. */ public SnuggleEngine(StylesheetManager stylesheetManager) { this.packages = new ArrayList<SnugglePackage>(); this.defaultSessionConfiguration = new SessionConfiguration(); this.defaultDOMOutputOptions = new DOMOutputOptions(); this.defaultXMLStringOutputOptions = new XMLStringOutputOptions(); /* Create manager for XSLT stlyesheets using the given cache */ this.stylesheetManager = stylesheetManager; /* Add in core package */ packages.add(CorePackageDefinitions.getPackage()); }
/** * Creates a {@link SnuggleSnapshot} Object holding the current state of this session that can * be later used to recreate a session having exactly the same state. */ public SnuggleSnapshot createSnapshot() { return new SnuggleSnapshot(engine, packages, (SessionConfiguration) configuration.clone(), (DOMOutputOptions) defaultDOMOutputOptions.clone(), /* (No real need to clone as this is immutable here, but we'll do it for consistency) */ (XMLStringOutputOptions) defaultXMLStringOutputOptions.clone(), /* (Ditto) */ new ArrayList<InputError>(errors), new HashMap<String, UserDefinedCommand>(userCommandMap), new HashMap<String, UserDefinedEnvironment>(userEnvironmentMap), new ArrayList<FlowToken>(parsedTokens)); }
/** * Creates a new {@link SnuggleEngine} using the given {@link StylesheetManager} * for managing stylesheets. Use this if you want to integrate XSLT caching * with your own code, want more control over how things get cached or want to control * the selection of XSLT implementations. */ public SnuggleEngine(StylesheetManager stylesheetManager) { this.packages = new ArrayList<SnugglePackage>(); this.defaultSessionConfiguration = new SessionConfiguration(); this.defaultDOMOutputOptions = new DOMOutputOptions(); this.defaultXMLStringOutputOptions = new XMLStringOutputOptions(); /* Create manager for XSLT stlyesheets using the given cache */ this.stylesheetManager = stylesheetManager; /* Add in core package */ packages.add(CorePackageDefinitions.getPackage()); }
this.configuration = (SessionConfiguration) configuration.clone();