Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2005,2009 Ivan SZKIBA
   *
   * 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 org.ini4j.spi;
 
 
 import java.io.Reader;
 
 import java.net.URL;
 
 class IniSource
 {
     public static final char INCLUDE_BEGIN = '<';
     public static final char INCLUDE_END = '>';
     public static final char INCLUDE_OPTIONAL = '?';
     private static final char ESCAPE_CHAR = '\\';
     private URL _base;
     private IniSource _chain;
     private final String _commentChars;
     private final Config _config;
     private final HandlerBase _handler;
     private final LineNumberReader _reader;
 
     IniSource(InputStream inputHandlerBase handlerString commentsConfig config)
     {
         this(new UnicodeInputStreamReader(inputconfig.getFileEncoding()), handlercommentsconfig);
     }
 
     IniSource(Reader inputHandlerBase handlerString commentsConfig config)
     {
          = new LineNumberReader(input);
          = handler;
          = comments;
          = config;
     }
 
     IniSource(URL inputHandlerBase handlerString commentsConfig configthrows IOException
     {
         this(new UnicodeInputStreamReader(input.openStream(), config.getFileEncoding()), handlercommentsconfig);
          = input;
     }
 
     int getLineNumber()
     {
         int ret;
 
         if ( == null)
         {
             ret = .getLineNumber();
         }
         else
         {
             ret = .getLineNumber();
         }
 
         return ret;
     }
 
     String readLine() throws IOException
     {
         String line;
 
         if ( == null)
         {
             line = readLineLocal();
         }
         else
         {
             line = .readLine();
             if (line == null)
             {
                  = null;
                 line = readLine();
             }
         }
 
         return line;
     }
 
     private void close() throws IOException
     {
         .close();
     }
    private int countEndingEscapes(String line)
    {
        int escapeCount = 0;
        for (int i = line.length() - 1; (i >= 0) && (line.charAt(i) == ); i--)
        {
            escapeCount++;
        }
        return escapeCount;
    }
    private void handleComment(StringBuilder buff)
    {
        if (buff.length() != 0)
        {
            buff.deleteCharAt(buff.length() - 1);
            .handleComment(buff.toString());
            buff.delete(0, buff.length());
        }
    }
    private String handleInclude(String inputthrows IOException
    {
        String line = input;
        if (.isInclude() && (line.length() > 2) && (line.charAt(0) == ) && (line.charAt(line.length() - 1) == ))
        {
            line = line.substring(1, line.length() - 1).trim();
            boolean optional = line.charAt(0) == ;
            if (optional)
            {
                line = line.substring(1).trim();
            }
            URL loc = ( == null) ? new URL(line) : new URL(line);
            if (optional)
            {
                try
                {
                     = new IniSource(loc);
                }
                catch (IOException x)
                {
                    assert true;
                }
                finally
                {
                    line = readLine();
                }
            }
            else
            {
                 = new IniSource(loc);
                line = readLine();
            }
        }
        return line;
    }
    private String readLineLocal() throws IOException
    {
        String line = readLineSkipComments();
        if (line == null)
        {
            close();
        }
        else
        {
            line = handleInclude(line);
        }
        return line;
    }
    private String readLineSkipComments() throws IOException
    {
        String line;
        StringBuilder comment = new StringBuilder();
        StringBuilder buff = new StringBuilder();
        for (line = .readLine(); line != nullline = .readLine())
        {
            line = line.trim();
            if (line.length() == 0)
            {
                handleComment(comment);
            }
            else if ((.indexOf(line.charAt(0)) >= 0) && (buff.length() == 0))
            {
                comment.append(line.substring(1));
                comment.append(.getLineSeparator());
            }
            else
            {
                handleComment(comment);
                if (!.isEscapeNewline() || ((countEndingEscapes(line) & 1) == 0))
                {
                    buff.append(line);
                    line = buff.toString();
                    break;
                }
                buff.append(line.subSequence(0, line.length() - 1));
            }
        }
        // handle end comments
        if ((line == null) && (comment.length() != 0))
        {
            handleComment(comment);
        }
        return line;
    }
New to GrepCode? Check out our FAQ X