All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cat.inspiracio.orange.Part Maven / Gradle / Ivy

/*    Copyright 2019 Alexander Bunkenburg

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package cat.inspiracio.orange;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/** Some classes that help parsing source strings. 
 * 
 * Part has subclasses Literal and Expression. 
 * An expression is like "${o.getName()}" and everything else 
 * is Literal.
 * 
 * For example, 
 * 
 * 	

Name: ${o.getName()}

* * is a sequence: * * Literal("

Name: ") * Expression("o.getName()") * Literal("

") * */ abstract class Part{ String s; Part(String s){this.s=s;} abstract boolean isLiteral(); String getLiteral(){return s;} String getExpression(){return s;} @Override public boolean equals(Object o){ if(o==null) return false; if(!(o instanceof Part)) return false; Part p=(Part)o; return isLiteral()==p.isLiteral() && s.equals(p.s); } /** Factory: parse a string into literals and expressions. */ static Listparse(String s) throws IOException{ //opt: use StringBuilder Listparts=new ArrayList(); int start=s.indexOf("${"); while(0<=start){ //check that it's not \${...} boolean escaped = 1<=start && s.charAt(start-1)=='\\'; if(escaped){ //Must output $ for \$ // s[start-1] = \ // s[start] = $ // s[start+1] = { //delete \ s = s.substring(0, start-1) + s.substring(start); // look for the next ${ start=s.indexOf("${", start); } else if(!escaped){ int end=s.indexOf("}", start); if(-1==end){ //String location=getLocation(source); String message="Unterminated expression: " + s;// + " at " + location; throw new IOException(message); } //s[end] = '}' String prefix=s.substring(0, start); if(0




© 2015 - 2024 Weber Informatics LLC | Privacy Policy