Class SourceCodeViewModel

java.lang.Object
io.jenkins.plugins.prism.SourceCodeViewModel
All Implemented Interfaces:
ModelObject

public class SourceCodeViewModel extends Object implements ModelObject
Renders a source code file with Prism syntax highlighting. Optionally, highlights an arbitrary number of markers in the source code: each marker is either a line, some characters in a line, or a multi-line block. The details of each marker (title, description, and icon) are shown in a box after the last line of the marker.

The model can be shown as a separate Jenkins view (see index.jelly), or it can be embedded into the view of another model by including the fragment content.jelly, e.g., <st:include it="${it.sourceCode}" page="content.jelly"/>. The fragment is available for the PermissionDeniedViewModel as well, so the result of the factory methods can be embedded directly.

A marker can be selected using the fragment of the URL: either the ID of the marker (see Marker.MarkerBuilder.withId(String)) or a line number. A selected marker is highlighted, its description is expanded, and the view scrolls to the marker. If there is only one marker, then this marker is selected automatically.

Author:
Ullrich Hafner
  • Constructor Details

    • SourceCodeViewModel

      @Deprecated public SourceCodeViewModel(Run<?,?> owner, String fileName, Reader sourceCodeReader, Marker marker)
      Deprecated.
      Creates a new source code view model instance.
      Parameters:
      owner - the current build as the owner of this view
      fileName - the file name of the shown content
      sourceCodeReader - the source code file to show, provided by a Reader instance
      marker - a block of lines (or a part of a line) to mark in the source code view
  • Method Details

    • hasPermissionToViewSourceCode

      public static boolean hasPermissionToViewSourceCode(Run<?,?> owner)
      Checks if the current user has permission to view source code.
      Parameters:
      owner - the current build as the owner of the view
      Returns:
      true if the user has permission, false otherwise
    • protectedSourceCodeView

      public static ModelObject protectedSourceCodeView(ModelObject view, Run<?,?> owner, String fileName)
      Protects a source code view by checking permissions.
      Parameters:
      view - the source code view to protect
      owner - the current build as the owner of the view
      fileName - the name of the file being viewed
      Returns:
      the protected view (either original view or PermissionDeniedViewModel)
    • create

      public static ModelObject create(Run<?,?> owner, String fileName, Reader sourceCodeReader, Marker marker)
      Creates a source code view model or a permission-denied view model based on the user's permissions. This is the recommended way to create a view model as it checks permissions before rendering source code.
      Parameters:
      owner - the current build as the owner of this view
      fileName - the file name of the shown content
      sourceCodeReader - the source code file to show, provided by a Reader instance
      marker - a block of lines (or a part of a line) to mark in the source code view
      Returns:
      a SourceCodeViewModel if permission is granted, or a PermissionDeniedViewModel otherwise
    • create

      public static ModelObject create(Run<?,?> owner, String fileName, Reader sourceCodeReader, List<Marker> markers)
      Creates a source code view model or a permission-denied view model based on the user's permissions. The view model highlights all specified markers.
      Parameters:
      owner - the current build as the owner of this view
      fileName - the file name of the shown content
      sourceCodeReader - the source code file to show, provided by a Reader instance
      markers - the blocks of lines (or parts of lines) to mark in the source code view, may be empty
      Returns:
      a SourceCodeViewModel if permission is granted, or a PermissionDeniedViewModel otherwise
    • getPrismConfiguration

      public PrismConfiguration getPrismConfiguration()
    • getDisplayName

      public String getDisplayName()
      Specified by:
      getDisplayName in interface ModelObject
    • getOwner

      public Run<?,?> getOwner()
      Returns the build as the owner of this view.
      Returns:
      the build
    • getSourceCode

      public String getSourceCode()
      Returns the colorized source code.
      Returns:
      the source code
    • isCollapsible

      public boolean isCollapsible()
      Returns whether long sections of lines without markers are collapsed in the view. The lines around the markers are still visible, and the collapsed sections can be expanded by the user.
      Returns:
      true if the sections without markers are collapsed, false if all lines are visible