FormatError.java

  1. /*
  2.  * Copyright (C) 2008, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.patch;

  11. import static java.nio.charset.StandardCharsets.UTF_8;

  12. import java.util.Locale;

  13. import org.eclipse.jgit.util.RawParseUtils;

  14. /**
  15.  * An error in a patch script
  16.  */
  17. public class FormatError {
  18.     /** Classification of an error. */
  19.     public enum Severity {
  20.         /** The error is unexpected, but can be worked around. */
  21.         WARNING,

  22.         /** The error indicates the script is severely flawed. */
  23.         ERROR;
  24.     }

  25.     private final byte[] buf;

  26.     private final int offset;

  27.     private final Severity severity;

  28.     private final String message;

  29.     FormatError(final byte[] buffer, final int ptr, final Severity sev,
  30.             final String msg) {
  31.         buf = buffer;
  32.         offset = ptr;
  33.         severity = sev;
  34.         message = msg;
  35.     }

  36.     /**
  37.      * Get the severity of the error.
  38.      *
  39.      * @return the severity of the error.
  40.      */
  41.     public Severity getSeverity() {
  42.         return severity;
  43.     }

  44.     /**
  45.      * Get a message describing the error.
  46.      *
  47.      * @return a message describing the error.
  48.      */
  49.     public String getMessage() {
  50.         return message;
  51.     }

  52.     /**
  53.      * Get the byte buffer holding the patch script.
  54.      *
  55.      * @return the byte buffer holding the patch script.
  56.      */
  57.     public byte[] getBuffer() {
  58.         return buf;
  59.     }

  60.     /**
  61.      * Get byte offset within {@link #getBuffer()} where the error is.
  62.      *
  63.      * @return byte offset within {@link #getBuffer()} where the error is.
  64.      */
  65.     public int getOffset() {
  66.         return offset;
  67.     }

  68.     /**
  69.      * Get line of the patch script the error appears on.
  70.      *
  71.      * @return line of the patch script the error appears on.
  72.      */
  73.     public String getLineText() {
  74.         final int eol = RawParseUtils.nextLF(buf, offset);
  75.         return RawParseUtils.decode(UTF_8, buf, offset, eol);
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     public String toString() {
  80.         final StringBuilder r = new StringBuilder();
  81.         r.append(getSeverity().name().toLowerCase(Locale.ROOT));
  82.         r.append(": at offset "); //$NON-NLS-1$
  83.         r.append(getOffset());
  84.         r.append(": "); //$NON-NLS-1$
  85.         r.append(getMessage());
  86.         r.append("\n"); //$NON-NLS-1$
  87.         r.append("  in "); //$NON-NLS-1$
  88.         r.append(getLineText());
  89.         return r.toString();
  90.     }
  91. }