/** Creates a full diagnosis of the given error */ public static String formatErrorAsString(InputError error) { StringBuffer resultBuilder = new StringBuffer(); appendErrorAsString(resultBuilder, error); return resultBuilder.toString(); }
public static void appendErrorAsString(StringBuffer messageBuilder, InputError error) { new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("error_as_string")).format(new Object[] { formatErrorCodeName(error.getErrorCode()), /* Error code/package */ getErrorMessage(error) /* Error Message */ }, messageBuilder, null); FrozenSlice errorSlice = error.getSlice(); if (errorSlice!=null) { appendSliceContext(messageBuilder, errorSlice); } }
public SnuggleParseException(InputError error) { super(MessageFormatter.formatErrorAsString(error)); this.error = error; }
private static void appendFrame(StringBuffer messageBuilder, CharacterSource source, int offsetInSource) { SourceContext context = source.context; if (context instanceof SnuggleInputReader) { SnuggleInputReader inputContext = (SnuggleInputReader) context; int[] location = inputContext.getLineAndColumn(offsetInSource); appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("input_context")).format(new Object[] { location[0], /* Line */ location[1], /* Column */ inputContext.getInput().getIdentifier() /* Input description */ }, messageBuilder, null); } else if (context instanceof WorkingDocument.SubstitutionContext) { WorkingDocument.SubstitutionContext substitutionContext = (WorkingDocument.SubstitutionContext) context; appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("subs_context")).format(new Object[] { offsetInSource, /* Character index */ formatText(source.substitutedText), /* Before subs */ formatText(substitutionContext.replacement) /* After subs */ }, messageBuilder, null); } else { throw new SnuggleLogicException("Unexpected SourceContext " + context.getClass().getName()); } if (source.substitutedSource!=null) { appendFrame(messageBuilder, source.substitutedSource, source.substitutionOffset); } }
/** * Creates a DOM {@link Element} containing information about the given error, including * either just the {@link ErrorCode} or full details. * * @param ownerDocument {@link Document} that will contain the resulting element. * @param error {@link InputError} to format * @param fullDetails false if you just want the error code, true for full details. */ public static Element formatErrorAsXML(Document ownerDocument, InputError error, boolean fullDetails) { Element result = ownerDocument.createElementNS(SnuggleConstants.SNUGGLETEX_NAMESPACE, "error"); result.setAttribute("code", error.getErrorCode().getName()); result.setAttribute("package", error.getErrorCode().getErrorGroup().getPackage().getName()); if (fullDetails) { /* Nicely format XML error content */ StringBuffer messageBuilder = new StringBuffer(getErrorMessage(error)); FrozenSlice errorSlice = error.getSlice(); if (errorSlice!=null) { appendSliceContext(messageBuilder, errorSlice); } /* Add message as child node */ result.appendChild(ownerDocument.createTextNode(messageBuilder.toString())); } /* That's it! */ return result; }
case XML_SHORT: errorElement = MessageFormatter.formatErrorAsXML(document, errorToken.getError(), errorOptions==ErrorOutputOptions.XML_FULL); errorElement = MessageFormatter.formatErrorAsXHTML(document, errorToken.getError()); ancestorElement.appendChild(errorElement); break;
/** Constructs an error message for the given {@link ErrorCode} and arguments. */ public static String getErrorMessage(ErrorCode errorCode, Object... arguments) { ResourceBundle errorMessageBundle = errorCode.getErrorGroup().getPackage().getErrorMessageBundle(); String result; if (errorMessageBundle!=null) { /* Use ResourceBundle specified to format error */ result = MessageFormat.format(errorMessageBundle.getString(errorCode.toString()), arguments); } else { /* No ResourceBundle specified, so do as best as we can */ result = "Error " + formatErrorCodeName(errorCode) + ": " + Arrays.toString(arguments); } return result; }
/** Constructs an error message for the given {@link InputError}. */ public static String getErrorMessage(InputError error) { return getErrorMessage(error.getErrorCode(), error.getArguments()); }
public static void appendSliceContext(StringBuffer messageBuilder, FrozenSlice slice) { WorkingDocument document = slice.getDocument(); /* Work out where the error occurred */ IndexResolution errorResolution = document.resolveIndex(slice.startIndex, false); if (errorResolution==null) { /* (If this happens, then most likely the error occurred at the end of the document) */ errorResolution = document.resolveIndex(slice.startIndex, true); } if (errorResolution==null) { throw new SnuggleLogicException("Could not resolve component containing error slice starting at " + slice.startIndex); } Slice errorSlice = errorResolution.slice; CharacterSource errorComponent = errorSlice.resolvedComponent; int errorIndex = errorResolution.indexInComponent; appendFrame(messageBuilder, errorComponent, errorIndex); }
/** * Creates a DOM {@link Element} containing information about the given error, including * either just the {@link ErrorCode} or full details. * <p> * The error {@link Element} will have qualified name <tt>error</tt> and belong to the * {@link SnuggleConstants#SNUGGLETEX_NAMESPACE} namespace. * * @param ownerDocument {@link Document} that will contain the resulting element. * @param error {@link InputError} to format * @param fullDetails false if you just want the error code, true for full details. * * @throws DOMException */ public static Element formatErrorAsXML(Document ownerDocument, InputError error, boolean fullDetails) { return formatErrorAsXML(ownerDocument, null, error, fullDetails); }
private static void appendFrame(StringBuffer messageBuilder, CharacterSource source, int offsetInSource) { SourceContext context = source.context; if (context instanceof SnuggleInputReader) { SnuggleInputReader inputContext = (SnuggleInputReader) context; int[] location = inputContext.getLineAndColumn(offsetInSource); appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("input_context")).format(new Object[] { location[0], /* Line */ location[1], /* Column */ inputContext.getInput().getIdentifier() /* Input description */ }, messageBuilder, null); } else if (context instanceof WorkingDocument.SubstitutionContext) { WorkingDocument.SubstitutionContext substitutionContext = (WorkingDocument.SubstitutionContext) context; appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("subs_context")).format(new Object[] { offsetInSource, /* Character index */ formatText(source.substitutedText), /* Before subs */ formatText(substitutionContext.replacement) /* After subs */ }, messageBuilder, null); } else { throw new SnuggleLogicException("Unexpected SourceContext " + context.getClass().getName()); } if (source.substitutedSource!=null) { appendFrame(messageBuilder, source.substitutedSource, source.substitutionOffset); } }
/** * Creates a DOM {@link Element} containing information about the given error, including * either just the {@link ErrorCode} or full details. * * @param ownerDocument {@link Document} that will contain the resulting element. * @param error {@link InputError} to format * @param fullDetails false if you just want the error code, true for full details. */ public static Element formatErrorAsXML(Document ownerDocument, InputError error, boolean fullDetails) { Element result = ownerDocument.createElementNS(SnuggleConstants.SNUGGLETEX_NAMESPACE, "error"); result.setAttribute("code", error.getErrorCode().getName()); result.setAttribute("package", error.getErrorCode().getErrorGroup().getPackage().getName()); if (fullDetails) { /* Nicely format XML error content */ StringBuffer messageBuilder = new StringBuffer(getErrorMessage(error)); FrozenSlice errorSlice = error.getSlice(); if (errorSlice!=null) { appendSliceContext(messageBuilder, errorSlice); } /* Add message as child node */ result.appendChild(ownerDocument.createTextNode(messageBuilder.toString())); } /* That's it! */ return result; }
case XML_SHORT: errorElement = MessageFormatter.formatErrorAsXML(document, options, errorToken.getError(), errorElement = MessageFormatter.formatErrorAsXHTML(document, errorToken.getError()); ancestorElement.appendChild(errorElement); break;
/** Constructs an error message for the given {@link ErrorCode} and arguments. */ public static String getErrorMessage(ErrorCode errorCode, Object... arguments) { ResourceBundle errorMessageBundle = errorCode.getErrorGroup().getPackage().getErrorMessageBundle(); String result; if (errorMessageBundle!=null) { /* Use ResourceBundle specified to format error */ result = MessageFormat.format(errorMessageBundle.getString(errorCode.toString()), arguments); } else { /* No ResourceBundle specified, so do as best as we can */ result = "Error " + formatErrorCodeName(errorCode) + ": " + Arrays.toString(arguments); } return result; }
/** Constructs an error message for the given {@link InputError}. */ public static String getErrorMessage(InputError error) { return getErrorMessage(error.getErrorCode(), error.getArguments()); }
public static void appendSliceContext(StringBuffer messageBuilder, FrozenSlice slice) { WorkingDocument document = slice.getDocument(); /* Work out where the error occurred */ IndexResolution errorResolution = document.resolveIndex(slice.startIndex, false); if (errorResolution==null) { /* (If this happens, then most likely the error occurred at the end of the document) */ errorResolution = document.resolveIndex(slice.startIndex, true); } if (errorResolution==null) { throw new SnuggleLogicException("Could not resolve component containing error slice starting at " + slice.startIndex); } Slice errorSlice = errorResolution.slice; CharacterSource errorComponent = errorSlice.resolvedComponent; int errorIndex = errorResolution.indexInComponent; appendFrame(messageBuilder, errorComponent, errorIndex); }
public static void appendErrorAsString(StringBuffer messageBuilder, InputError error) { new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("error_as_string")).format(new Object[] { formatErrorCodeName(error.getErrorCode()), /* Error code/package */ getErrorMessage(error) /* Error Message */ }, messageBuilder, null); FrozenSlice errorSlice = error.getSlice(); if (errorSlice!=null) { appendSliceContext(messageBuilder, errorSlice); } }
private static void appendFrame(StringBuffer messageBuilder, CharacterSource source, int offsetInSource) { SourceContext context = source.context; if (context instanceof SnuggleInputReader) { SnuggleInputReader inputContext = (SnuggleInputReader) context; int[] location = inputContext.getLineAndColumn(offsetInSource); appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("input_context")).format(new Object[] { location[0], /* Line */ location[1], /* Column */ inputContext.getInput().getIdentifier() /* Input description */ }, messageBuilder, null); } else if (context instanceof WorkingDocument.SubstitutionContext) { WorkingDocument.SubstitutionContext substitutionContext = (WorkingDocument.SubstitutionContext) context; appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("subs_context")).format(new Object[] { offsetInSource, /* Character index */ formatText(source.substitutedText), /* Before subs */ formatText(substitutionContext.replacement) /* After subs */ }, messageBuilder, null); } else { throw new SnuggleLogicException("Unexpected SourceContext " + context.getClass().getName()); } if (source.substitutedSource!=null) { appendFrame(messageBuilder, source.substitutedSource, source.substitutionOffset); } }
StringBuffer messageBuilder = new StringBuffer(getErrorMessage(error)); FrozenSlice errorSlice = error.getSlice(); if (errorSlice!=null) { appendSliceContext(messageBuilder, errorSlice);
case XML_SHORT: errorElement = MessageFormatter.formatErrorAsXML(document, errorToken.getError(), errorOptions==ErrorOutputOptions.XML_FULL); errorElement = MessageFormatter.formatErrorAsXHTML(document, errorToken.getError()); ancestorElement.appendChild(errorElement); break;