1 /* 2 * $Header$ 3 * $Revision$ 4 * $Date$ 5 * 6 * ==================================================================== 7 * 8 * Copyright 2000-2002 bob mcwhirter & James Strachan. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions are 13 * met: 14 * 15 * * Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 18 * * Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * * Neither the name of the Jaxen Project nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 29 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 30 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 * 38 * ==================================================================== 39 * This software consists of voluntary contributions made by many 40 * individuals on behalf of the Jaxen Project and was originally 41 * created by bob mcwhirter <bob@werken.com> and 42 * James Strachan <jstrachan@apache.org>. For more information on the 43 * Jaxen Project, please see <http://www.jaxen.org/>. 44 * 45 * $Id$ 46 */ 47 48 49 package org.jaxen; 50 51 /** Indicates an error during parsing of an XPath expression. 52 * 53 * @author <a href="mailto:bob@werken.com">bob mcwhirter</a> 54 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 55 */ 56 public class XPathSyntaxException extends JaxenException 57 { 58 /** 59 * 60 */ 61 private static final long serialVersionUID = 1980601567207604059L; 62 63 /** The textual XPath expression */ 64 private String xpath; 65 66 /** The position of the error */ 67 private int position; 68 69 /** 70 * Create a new XPathSyntaxException wrapping an existing 71 * <code>org.jaxen.saxpath.XPathSyntaxException</code>. 72 * 73 * @param e the exception that caused this exception 74 */ 75 public XPathSyntaxException(org.jaxen.saxpath.XPathSyntaxException e) 76 { 77 super( e ); 78 79 this.xpath = e.getXPath(); 80 this.position = e.getPosition(); 81 } 82 83 /** Constructor 84 * 85 * @param xpath the erroneous XPath expression 86 * @param position the position of the error 87 * @param message the error message 88 */ 89 public XPathSyntaxException(String xpath, 90 int position, 91 String message) 92 { 93 super( message ); 94 95 this.xpath = xpath; 96 this.position = position; 97 } 98 99 /** Retrieve the position of the error. 100 * 101 * @return the position of the error 102 */ 103 public int getPosition() 104 { 105 return this.position; 106 } 107 108 /** Retrieve the expression containing the error. 109 * 110 * @return the erroneous expression 111 */ 112 public String getXPath() 113 { 114 return this.xpath; 115 } 116 117 /** Retrieve a string useful for denoting where 118 * the error occurred. 119 * 120 * <p> 121 * This is a string composed of whitespace and 122 * a marker at the position (see {@link #getPosition}) 123 * of the error. This is useful for creating 124 * friendly multi-line error displays. 125 * </p> 126 * 127 * @return the error position marker 128 */ 129 public String getPositionMarker() 130 { 131 StringBuffer buf = new StringBuffer(); 132 133 int pos = getPosition(); 134 135 for ( int i = 0 ; i < pos ; ++i ) 136 { 137 buf.append(" "); 138 } 139 140 buf.append("^"); 141 142 return buf.toString(); 143 144 } 145 146 /** Retrieve the friendly multi-line error message. 147 * 148 * <p> 149 * This returns a multi-line string that contains 150 * the original erroneous XPath expression with a 151 * marker underneath indicating exactly where the 152 * error occurred. 153 * </p> 154 * 155 * @return the multi-line error message 156 */ 157 public String getMultilineMessage() 158 { 159 StringBuffer buf = new StringBuffer(getMessage()); 160 buf.append( "\n" ); 161 buf.append( getXPath() ); 162 buf.append( "\n" ); 163 164 buf.append( getPositionMarker() ); 165 166 return buf.toString(); 167 } 168 }