Java Code Examples for java.io.PushbackReader#read()

The following examples show how to use java.io.PushbackReader#read() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 6 votes vote down vote up
private Row readLine(PushbackReader in) throws IOException {
    Row ret=new Row();
    while(true){
        int c=in.read();
        if(c<0) {
            if(ret.size()>0) return ret;
            else return null;
        }
        else in.unread(c);

        Object v=readValue(in);
        ret.add(v);
        boolean next=readValueSeparator(in);
        if(!next) break;
    }

    return ret;
}
 
Example 2
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 6 votes vote down vote up
private String parseString(PushbackReader in) throws IOException {
    readWhitespace(in);

    int c=in.read();
    if(c!=stringChar) throw new RuntimeException("Error parsing CSV file");
    StringBuilder sb=new StringBuilder();

    while(true){
        c=in.read();
        if(c<0) throw new RuntimeException("Error parsing CSV file");
        else if(c == stringChar) {
            int c2=in.read();
            if(c2==stringChar){
                sb.append((char)stringChar);
            }
            else {
                if(c2>=0) in.unread(c2);
                return sb.toString();
            }
        }
        else {
            sb.append((char)c);
        }
    }
}
 
Example 3
Source File: OFXTransactionExtracter.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
private static String getNextValue(PushbackReader stream)
		throws IOException {
	StringBuffer value = new StringBuffer();
	int ch = 0;

	while ((ch = stream.read()) != -1) {
		if (ch == START_TAG) {
			stream.unread(ch);
			break;
		}

		value.append((char) ch);
	}

	return value.toString().trim();
}
 
Example 4
Source File: IonBinary.java    From ion-java with Apache License 2.0 6 votes vote down vote up
final boolean isLongTerminator(int terminator, PushbackReader r) throws IOException {
    int c;

    // look for terminator 2 - if it's not there put back what we saw
    c = r.read();
    if (c != terminator) {
        r.unread(c);
        return false;
    }

    // look for terminator 3 - otherwise put back what we saw and the
    // preceeding terminator we found above
    c = r.read();
    if (c != terminator) {
        r.unread(c);
        r.unread(terminator);
        return false;
    }
    // we found 3 in a row - now that's a long terminator
    return true;
}
 
Example 5
Source File: DTDParser.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Parser that eats everything until two consecutive dashes (inclusive) */
private void parseComment( PushbackReader in ) throws IOException, WrongDTDException {
    int state = COMM_TEXT;
    for( ;; ) {
        int i = in.read();
        if( i == -1 ) break; // EOF
        switch( state ) {
            case COMM_TEXT:
                if( i == '-' ) state = COMM_DASH;
                break;
            case COMM_DASH:
                if( i == '-' ) return; // finished eating comment
                state = COMM_TEXT;
                break;
        }
    }
    throw new WrongDTDException( "Premature end of DTD" ); // NOI18N
}
 
Example 6
Source File: DTDParser.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Parser that reads the name of entity reference and replace it with
 * the content of that entity (using the pushback capability of input).
 * It gets the control just after starting '%'
 * @returns the name of reference which was replaced. */
private String parseEntityReference( PushbackReader in ) throws IOException, WrongDTDException {
    StringBuffer sb = new StringBuffer();
    for( ;; ) {
        int i = in.read();
        if( i == -1 ) break; // EOF
        if( isNameChar( (char)i ) ) {
            sb.append( (char)i );                 // next char of name
        } else {
            String entValue = (String)entityMap.get( sb.toString() ); //get the entity content
            if( entValue == null )
                throw new WrongDTDException( "No such entity: \"" + sb + "\"" ); // NOI18N
            
            if( i != ';' ) in.unread( i );
            in.unread( entValue.toCharArray() );  // push it back to stream
            return sb.toString();
        }
    }
    throw new WrongDTDException( "Premature end of DTD" ); // NOI18N
}
 
Example 7
Source File: IonTokenReader.java    From ion-java with Apache License 2.0 6 votes vote down vote up
public static int readDigit(/*EscapedCharacterReader*/PushbackReader r, int radix, boolean isRequired) throws IOException {
    int c = r.read();
    if (c < 0) {
        r.unread(c);
        return -1;
    }

    int c2 =  Character.digit(c, radix);
    if (c2 < 0) {
        if (isRequired) {
            throw new IonException("bad digit in escaped character '"+((char)c)+"'");
        }
        // if it's not a required digit, we just throw it back
        r.unread(c);
        return -1;
    }
    return c2;
}
 
Example 8
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 6 votes vote down vote up
private Object readValue(PushbackReader in) throws IOException {
    readWhitespace(in);
    while(true){
        int c=in.read();
        if(c==valueSeparator || c==lineSeparator) {
            in.unread(c);
            return null;
        }
        if(c<0){
            return null;
        }
        else if(c==stringChar){
            in.unread(c);
            return parseString(in);
        }
        else {
            in.unread(c);
            return parseNumberOrDate(in);
        }
    }
}
 
Example 9
Source File: XmlReader.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void throwIfNotExpectedChar(char c,
                                      char expected,
                                      PushbackReader pbr)
    throws Exception
{
  if (c != expected) {
    StringBuffer msg = new StringBuffer();
    msg.append("Expected " + expected + " but found " + c + "\n");
    msg.append("At:");
    for (int i = 0; i < 20; ++i) {
      int rc = pbr.read();
      if (rc == -1) {
        break;
      }
      msg.append((char) rc);
    }
    throw new Exception(msg.toString()); // TODO
  }
}
 
Example 10
Source File: OFXTransactionExtracter.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
private static String getNextTag(PushbackReader stream) throws IOException {
	String tag = null;
	int ch = 0;

	while ((ch = stream.read()) != -1) {
		if (ch == START_TAG) {
			tag = getTagName(stream);
			break;
		}
	}

	return tag;
}
 
Example 11
Source File: HashTrie.java    From symbolicautomata with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively lookup the longest key match.
 * @param keyIn Where to read the key from
 * @param pos The position in the key that is being
 *	looked up at this level.
 * @return The Entry assocatied with the longest key
 *	match or null if none exists.
 */
Entry<T> getLongestMatch(PushbackReader keyIn, StringBuilder key) throws IOException
{
	Node<T> nextNode;
	Entry<T> ret;
	int c;
	char ch;
	int prevLen;

	// read next key char and append to key...
	if((c = keyIn.read())<0)
		// end of input, return what we have currently
		return Entry.newInstanceIfNeeded(key,value);
	ch = (char)c;
	prevLen = key.length();
	key.append(ch);

	if((nextNode = getNextNode(ch))==null)
	{	// last in trie... return ourselves
		return Entry.newInstanceIfNeeded(key,value);
	}
	if((ret = nextNode.getLongestMatch(keyIn, key))!=null)
		return ret;

	// undo reading of key char and appending to key...
	key.setLength(prevLen);
	keyIn.unread(c);

	return Entry.newInstanceIfNeeded(key,value);
}
 
Example 12
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
private void readWhitespace(PushbackReader in) throws IOException {
    while(true){
        int c=in.read();
        if(c<0) return;
        else if(c!=lineSeparator && Character.isWhitespace(c)) continue;
        else {
            in.unread(c);
            return;
        }
    }
}
 
Example 13
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
private String parseIntPart(PushbackReader in) throws IOException {
    StringBuilder sb=new StringBuilder();
    while(true){
        int c=in.read();
        if(c>='0' && c<='9'){
            sb.append((char)c);
        }
        else {
            if(c>=0) in.unread(c);
            if(sb.length()==0) return null;
            else return sb.toString();
        }
    }
}
 
Example 14
Source File: Patch.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Reads a line and returns the char sequence for newline */
private static String readLine(PushbackReader r, StringBuffer nl) throws IOException {
    StringBuffer line = new StringBuffer();
    int ic = r.read();
    if (ic == -1) return null;
    char c = (char) ic;
    while (c != '\n' && c != '\r') {
        line.append(c);
        ic = r.read();
        if (ic == -1) break;
        c = (char) ic;
    }
    if (nl != null) {
        nl.append(c);
    }
    if (c == '\r') {
        try {
            ic = r.read();
            if (ic != -1) {
                c = (char) ic;
                if (c != '\n') r.unread(c);
                else if (nl != null) nl.append(c);
            }
        } catch (IOException ioex) {}
    }
    return line.toString();
}
 
Example 15
Source File: CSVParser.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
private boolean readValueSeparator(PushbackReader in) throws IOException {
    readWhitespace(in);
    while(true) {
        int c=in.read();
        if(c==valueSeparator) return true;
        else if(c==lineSeparator || c<0) return false;
        else {
            throw new RuntimeException("Error parsing CSV file");
        }
    }
}
 
Example 16
Source File: OFXTransactionExtracter.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
private static String getTagName(PushbackReader stream) throws IOException {
	StringBuffer tag = new StringBuffer();
	int ch = 0;

	while ((ch = stream.read()) != -1) {
		if (ch == END_TAG) {
			break;
		}

		tag.append((char) ch);
	}

	return tag.toString().trim();
}
 
Example 17
Source File: HashTrie.java    From rhizobia_J with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Recursively lookup the longest key match.
 * @param keyIn Where to read the key from
 * @param pos The position in the key that is being
 *	looked up at this level.
 * @return The Entry assocatied with the longest key
 *	match or null if none exists.
 */
Entry<T> getLongestMatch(PushbackReader keyIn, StringBuilder key) throws IOException
{
	Node<T> nextNode;
	Entry<T> ret;
	int c;
	char ch;
	int prevLen;

	// read next key char and append to key...
	if((c = keyIn.read())<0)
		// end of input, return what we have currently
		return Entry.newInstanceIfNeeded(key,value);
	ch = (char)c;
	prevLen = key.length();
	key.append(ch);

	if((nextNode = getNextNode(ch))==null)
	{	// last in trie... return ourselves
		return Entry.newInstanceIfNeeded(key,value);
	}
	if((ret = nextNode.getLongestMatch(keyIn, key))!=null)
		return ret;

	// undo reading of key char and appending to key...
	key.setLength(prevLen);
	keyIn.unread(c);

	return Entry.newInstanceIfNeeded(key,value);
}
 
Example 18
Source File: XStringBufBase.java    From ModTheSpire with MIT License 4 votes vote down vote up
/**
 * Parse the next four characters and attempt to decode them as a Unicode
 * character code.
 *
 * @param pb  a PushbackReader representing the remainder of the region
 *            being processed (necessary for Unicode sequences)
 *
 * @return the decoded character, -1 on EOF, -2 for a bad Unicode sequence.
 *         If -2 is returned, the 4-character Unicode code is pushed
 *         back on the input stream. (The leading backslash and "u" are
 *         not pushed back, however).
 *
 * @throws IOException  on error
 */
private int decodeUnicodeSequence (PushbackReader pb)
    throws IOException
{
    int            c          = -1;
    boolean        incomplete = false;
    StringBuilder  buf        = new StringBuilder();

    // Read four characters, each of which represents a single hex
    // digit.

    for (int i = 0; i < 4; i++)
    {
        if ( (c = pb.read()) == -1 )
        {
            // Incomplete Unicode escape sequence at EOF. Just swallow
            // it.

            incomplete = true;
            break;
        }

        buf.append ((char) c);
    }

    if (incomplete)
    {
        // Push the entire buffered sequence back onto the input
        // stream.

        unread (buf.toString(), pb);
    }

    else
    {
        int      code = 0;
        boolean  error = false;

        try
        {
            code = Integer.parseInt (buf.toString(), 16);
            if (code < 0)
                throw new NumberFormatException();
        }

        catch (NumberFormatException ex)
        {
            // Bad hexadecimal value in Unicode escape sequence. Push
            // it all back.

            unread (buf.toString(), pb);
            error = true;
        }

        if (! Character.isDefined ((char) code))
        {
            // Invalid Unicode character. Push it all back.

            unread (buf.toString(), pb);
            error = true;
        }

        c = (error ? -2 : ((char) code));
    }

    return c;
}
 
Example 19
Source File: DTDParser.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Parser that takes care of conditional inclusion/exclusion of part
 * of DTD. Gets the control just after "<![" */
private void parseOptional( PushbackReader in ) throws IOException, WrongDTDException {
    int state = OPT_INIT;
    StringBuffer process = new StringBuffer();
    StringBuffer content = new StringBuffer();
    boolean ignore = false;
    
    for( ;; ) {
        int i = in.read();
        if( i == -1 ) break; // EOF
        switch( state ) {
            case OPT_INIT:
                if( Character.isWhitespace( (char)i ) ) break;
                if( i == '%' ) {
                    parseEntityReference( in );
                    break;
                }
                process.append( (char)i );
                state = OPT_PROCESS;
                break;
                
            case OPT_PROCESS:
                if( Character.isWhitespace( (char)i ) ) {
                    String s = process.toString();
                    if( "IGNORE".equals( s ) ) ignore = true; // NOI18N
                    else if( ! "INCLUDE".equals( s ) ) throw new WrongDTDException( "Unexpected processing instruction " + s ); // NOI18N
                    state = OPT_APROCESS;
                } else {
                    process.append( (char)i );
                }
                break;
                
            case OPT_APROCESS:
                if( Character.isWhitespace( (char)i ) ) break;
                if( i == '[' ) state = OPT_CONTENT;
                else throw new WrongDTDException( "Unexpected char '" + (char)i + "' in processing instruction." ); // NOI18N
                break;
                
            case OPT_CONTENT:
                if( i == ']' ) state = OPT_BRAC1;
                else content.append( (char)i );
                break;
                
            case OPT_BRAC1:
                if( i == ']' ) state = OPT_BRAC2;
                else {
                    content.append( ']' ).append( (char)i );
                    state = OPT_CONTENT;
                }
                break;
                
            case OPT_BRAC2:
                if( Character.isWhitespace( (char)i ) ) break;
                if( i == '>' ) {
                    if( !ignore ) in.unread( content.toString().toCharArray() );
                    return;
                }
                throw new WrongDTDException( "Unexpected char '" + (char)i + "' in processing instruction." ); // NOI18N
        }
    }
    
}
 
Example 20
Source File: SchemaParser.java    From pepper-box with Apache License 2.0 2 votes vote down vote up
/**
  * Function gets schema template as input and translates it into series of java statements
  * @param inputSchema
  * @return
  * @throws IOException
  */
public String getProcessedSchema(String inputSchema) throws PepperBoxException {

    try {
        PushbackReader pushbackReader = new PushbackReader(new StringReader(inputSchema), 80);

        int chr;

        StringBuilder token = new StringBuilder();

        StringBuilder processedSchema = new StringBuilder();

        processedSchema.append(PropsKeys.STR_BUILD_OBJ);

        while ((chr = pushbackReader.read()) != -1) {

            if (chr != PropsKeys.OPENING_BRACE) {

                token.append((char) chr);

            } else if ((chr = pushbackReader.read()) != PropsKeys.OPENING_BRACE) {

                pushbackReader.unread(chr);
                token.append(PropsKeys.OPENING_BRACE);

            } else {

                appendStaticString(token, processedSchema);
                token.delete(0, token.length());
                chr = pushbackReader.read();

                while (chr != -1) {

                    if (chr != PropsKeys.CLOSING_BRACE) {

                        token.append((char) chr);
                        chr = pushbackReader.read();

                    } else if ((chr = pushbackReader.read()) != PropsKeys.CLOSING_BRACE) {

                        pushbackReader.unread((char) chr);
                        token.append(PropsKeys.CLOSING_BRACE);

                    } else {

                        processedSchema.append(PropsKeys.STR_APPEND);
                        processedSchema.append(token.toString().replaceAll(PropsKeys.ESC_QUOTE, PropsKeys.DOUBLE_ESC_QUOTE));
                        processedSchema.append(PropsKeys.CLOSING_BRACKET);
                        token.delete(0, token.length());
                        break;

                    }
                }

            }
        }

        appendStaticString(token, processedSchema);
        processedSchema.append(PropsKeys.STR_TOSTRING);

        return processedSchema.toString().replaceAll(PropsKeys.STR_TAB, PropsKeys.ESCAPE_TAB).replaceAll(PropsKeys.STR_NEWLINE, PropsKeys.ESCAPE_NEWLINE).replaceAll(PropsKeys.STR_CARRIAGE_RETURN, PropsKeys.ESCAPE_CARRIAGE_RETURN);
    }catch (IOException e){
        throw new PepperBoxException(e);
    }
 }