/*******************************************************************************
 * Copyright 2017 xlate.io LLC, http://www.xlate.io
 *
 * 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 io.xlate.edi.stream;

public class EDIStreamException extends Exception {

    private static final long serialVersionUID = -1232370584780899896L;

    protected final transient Location location;

    /**
     * Construct an exception with the associated message.
     *
     * @param message
     *            the message to report
     */
    public EDIStreamException(String message) {
        super(message);
        location = null;
    }

    /**
     * Construct an exception with the associated exception
     *
     * @param cause
     *            a nested exception
     */
    public EDIStreamException(Throwable cause) {
        super(cause);
        location = null;
    }

    private static String displayLocation(Location location) {
        return location.getSegmentPosition() + " : " + location.getElementPosition();
    }

    /**
     * Construct an exception with the associated message, exception and
     * location.
     *
     * @param message
     *            the message to report
     * @param location
     *            the location of the error
     * @param cause
     *            a nested error / exception
     */
    public EDIStreamException(String message, Location location, Throwable cause) {
        super("EDIStreamException at [seg,ele]:[" + displayLocation(location) + "]\n" + "Message: "
                + message,
              cause);
        this.location = location;
    }

    /**
     * Construct an exception with the associated message, exception and
     * location.
     *
     * @param message
     *            the message to report
     * @param location
     *            the location of the error
     */
    public EDIStreamException(String message, Location location) {
        super("EDIStreamException at [seg,ele]:[" + displayLocation(location) + "]\n" + "Message: "
                + message);
        this.location = location;
    }

    /**
     * Gets the location of the exception
     *
     * @return the location of the exception, may be null if none is available
     */
    public Location getLocation() {
        return location;
    }
}

@@ -1533,82 +1533,947 @@
     
-return location.getSegmentPosition() + %22 : %22 + location.getElementPosition
+StringBuilder display = new StringBuilder();%0A%0A        display.append(%22segment %22);%0A%0A        if (location.getSegmentTag() != null) %7B%0A            display.append(location.getSegmentTag());%0A        %7D%0A%0A        display.append(%22 at position %22);%0A        display.append(String.valueOf(location.getSegmentPosition()));%0A%0A        if (location.getElementPosition() %3E -1) %7B%0A            display.append(%22, element %22);%0A            display.append(String.valueOf(location.getElementPosition()));%0A%0A            if (location.getElementOccurrence() %3E -1) %7B%0A                display.append(%22(occurrence %22);%0A                display.append(String.valueOf(location.getElementOccurrence()));%0A                display.append(')');%0A            %7D%0A        %7D%0A%0A        if (location.getComponentPosition() %3E -1) %7B%0A            display.append(%22, component %22);%0A            display.append(String.valueOf(location.getComponentPosition()));%0A        %7D%0A%0A        return display.toString
 ();%0A
@@ -2888,134 +2888,53 @@
 per(
-%22EDIStreamException at %5Bseg,ele%5D:%5B%22 + displayLocation(location) + %22%5D%5Cn%22 + %22Message: %22%0A                + message,%0A             
+message + %22 in %22 + displayLocation(location),
  cau
@@ -3306,119 +3306,52 @@
 per(
-%22EDIStreamException at %5Bseg,ele%5D:%5B%22 + displayLocation(location) + %22%5D%5Cn%22 + %22Message: %22%0A                + message
+message + %22 in %22 + displayLocation(location)
 );%0A 

Improve location of exception in EDIStreamException message