/*
 * IzPack - Copyright 2001-2010 Julien Ponge, All Rights Reserved.
 *
 * http://izpack.org/
 * http://izpack.codehaus.org/
 *
 * Copyright 2005,2009 Ivan SZKIBA
 * Copyright 2010,2011 Rene Krell
 *
 * 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 com.izforge.izpack.util.config.base.spi;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.util.Locale;

import com.izforge.izpack.util.config.base.Config;
import com.izforge.izpack.util.config.base.InvalidFileFormatException;

abstract class AbstractParser
{
    private final String _comments;
    private Config _config = Config.getGlobal();
    private final String _operators;

    protected AbstractParser(String operators, String comments)
    {
        _operators = operators;
        _comments = comments;
    }

    protected Config getConfig()
    {
        return _config;
    }

    protected void setConfig(Config value)
    {
        _config = value;
    }

    protected void parseError(String line, int lineNumber) throws InvalidFileFormatException
    {
        throw new InvalidFileFormatException("parse error (at line: " + lineNumber + "): " + line);
    }

    IniSource newIniSource(InputStream input, HandlerBase handler)
    {
        return new IniSource(input, handler, _comments, getConfig());
    }

    IniSource newIniSource(Reader input, HandlerBase handler)
    {
        return new IniSource(input, handler, _comments, getConfig());
    }

    IniSource newIniSource(URL input, HandlerBase handler) throws IOException
    {
        return new IniSource(input, handler, _comments, getConfig());
    }

    void parseOptionLine(String line, HandlerBase handler, int lineNumber) throws InvalidFileFormatException
    {
        int idx = indexOfOperator(line);
        String name = null;
        String value = null;

        if (idx < 0)
        {
            if (getConfig().isEmptyOption())
            {
                name = line;
            }
            else
            {
                parseError(line, lineNumber);
            }
        }
        else
        {
            name = unescapeFilter(line.substring(0, idx)).trim();
            value = unescapeFilter(line.substring(idx + 1)).trim();
        }

        if (name.length() == 0)
        {
            parseError(line, lineNumber);
        }

        if (getConfig().isLowerCaseOption())
        {
            name = name.toLowerCase(Locale.getDefault());
        }

        handler.handleOption(name, value);
    }

    String unescapeFilter(String line)
    {
        return getConfig().isEscape() ? EscapeTool.getInstance().unescape(line) : line;
    }

    protected int indexOfOperator(String line)
    {
        int idx = -1;
        // skip the operator characters that are enclosed within quotes
        // eg: "http://+:80/Temporary_Listen_Addresses/"=hex:
        int start = 0;
        boolean inQuotes = line.charAt(0) == '"';
        while( inQuotes ) {
        	start = line.indexOf('"', start + 1);
        	if( line.charAt(start - 1) != '\\' || line.charAt(start - 2) == '\\' ) {
        		inQuotes = false;
        		start++;
        	}
        }

        for (char c : _operators.toCharArray())
        {
            int index = line.indexOf(c, start);

            if ((index >= 0) && ((idx == -1) || (index < idx)))
            {
                idx = index;
            }
        }

        return idx;
    }
}

@@ -2489,24 +2489,54 @@
 ame = line;%0A
+                value = line;%0A
             
@@ -3572,16 +3572,40 @@
 t + 1);%0A
+    %09%09if (start %3E 2) %7B%0A%09
         
@@ -3674,24 +3674,25 @@
 == '%5C%5C' ) %7B%0A
+%09
         %09%09in
@@ -3707,16 +3707,17 @@
  false;%0A
+%09
         
@@ -3727,16 +3727,17 @@
 tart++;%0A
+%09
         
@@ -3739,31 +3739,139 @@
      %09%7D%0A    
-    %7D%0A%0A
+%09%09%7D%0A    %09%09else if (start %3C 0) %7B%0A        %09%09inQuotes = false;%0A    %09%09%7D%0A        %7D%0A%0A        if (start %3E -1) %0A        %7B%0A%09
         for 
@@ -3902,24 +3902,25 @@
 harArray())%0A
+%09
         %7B%0A  
@@ -3909,32 +3909,33 @@
 y())%0A%09        %7B%0A
+%09
             int 
@@ -3966,17 +3966,19 @@
 start);%0A
-%0A
+%09%0A%09
         
@@ -4033,16 +4033,17 @@
  idx)))%0A
+%09
         
@@ -4040,32 +4040,33 @@
 %0A%09            %7B%0A
+%09
                 
@@ -4078,16 +4078,19 @@
  index;%0A
+%09  
         
@@ -4083,32 +4083,41 @@
 x;%0A%09            
+%7D%0A%09      
   %7D%0A        %7D%0A%0A 
@@ -4106,32 +4106,40 @@
     %7D%0A        %7D%0A
+        
 %0A        return 

Fix for #361