/*
 * #%L
 * fujion
 * %%
 * Copyright (C) 2008 - 2017 Regenstrief Institute, Inc.
 * %%
 * 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.
 * 
 * #L%
 */
package org.fujion.page;

import org.w3c.dom.ProcessingInstruction;

/**
 * Abstract base class for implementing a parser for a processing instruction of a given target
 * type.
 */
public abstract class PIParserBase {
    
    private final String target;
    
    /**
     * Creates the PI parser with the specified target name.
     * 
     * @param target This is the name of the target of the processing instruction.
     */
    protected PIParserBase(String target) {
        this.target = target;
    }
    
    /**
     * Returns the processing instruction's target.
     * 
     * @return The processing instruction's target.
     */
    public String getTarget() {
        return target;
    }
    
    /**
     * Parses a processing instruction.
     * 
     * @param pi The processing instruction.
     * @param element The enclosing page element.
     */
    public abstract void parse(ProcessingInstruction pi, PageElement element);
    
    /**
     * Extracts an attribute from a processing instruction's data where the data is formatted as a
     * space-delimited list of key-value pairs.
     * 
     * @param pi The processing instruction.
     * @param name The attribute's key.
     * @param required If true and the attributes is not found, an exception is throw.
     * @return The attribute's value. If the attribute is not found, null will be returned unless
     *         the <code>required</code> parameter is true, in which case an exception is thrown.
     */
    protected String getAttribute(ProcessingInstruction pi, String name, boolean required) {
        String data = pi.getData();
        int i = data.indexOf(name + "=");
        data = data.substring(i + name.length() + 1);
        String dlm = data.startsWith("\"") || data.startsWith("'") ? data.substring(0, 1) : null;
        i = data.indexOf(dlm == null ? " " : dlm, 1);
        data = data.substring(dlm == null ? 0 : 1, i < 0 ? data.length() : i);
        data = data.isEmpty() ? null : data;
        
        if (data == null && required) {
            throw new IllegalArgumentException(
                    "Processing instruction \"" + pi.getTarget() + "\" is missing expected attribute \"" + name + "\"");
        }
        
        return data;
    }
    
}

@@ -262,18 +262,17 @@
 se at%0A *
- 
 %0A
+
  *      
@@ -316,17 +316,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -640,17 +640,16 @@
 ense.%0A *
- 
 %0A * #L%25%0A
@@ -871,21 +871,17 @@
 rBase %7B%0A
-    
 %0A
+
     priv
@@ -905,21 +905,17 @@
 target;%0A
-    
 %0A
+
     /**%0A
@@ -973,33 +973,32 @@
 get name.%0A     *
- 
 %0A     * @param t
@@ -1145,37 +1145,33 @@
 = target;%0A    %7D%0A
-    
 %0A
+
     /**%0A     * R
@@ -1212,33 +1212,32 @@
 s target.%0A     *
- 
 %0A     * @return 
@@ -1342,21 +1342,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     /**%0A
@@ -1389,33 +1389,32 @@
 truction.%0A     *
- 
 %0A     * @param p
@@ -1580,21 +1580,17 @@
 ement);%0A
-    
 %0A
+
     /**%0A
@@ -1742,17 +1742,16 @@
 .%0A     *
- 
 %0A     * 
@@ -2296,32 +2296,47 @@
 ;%0A        data =
+ i == -1 ? %22%22 :
  data.substring(
@@ -2635,25 +2635,17 @@
 : data;%0A
-        
 %0A
+
         
@@ -2859,17 +2859,9 @@
   %7D%0A
-        
 %0A
+
     
@@ -2883,15 +2883,11 @@
 ;%0A    %7D%0A
-    
 %0A%7D%0A

Fix PI parsing logic.