Class Validator


  • public class Validator
    extends java.lang.Object
    A simple validator for deployment descriptors.
    Author:
    Holger Eichelberger, SSE
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static interface  Validator.ValidatorFunction<O>
      A validation function, to point generically to the private validation functions defined by this class.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List<java.lang.String> messages  
      private java.util.Set<java.lang.String> serviceIds  
    • Constructor Summary

      Constructors 
      Constructor Description
      Validator()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.lang.String appendContext​(java.lang.String msg, java.lang.String msgContext)
      Appends the msgContext to msg for output.
      private java.lang.String appendToContext​(java.lang.String context, java.lang.String text)
      Appends text to context considering that context may be empty or null.
      private boolean assertCondition​(boolean condition, java.lang.String msg, java.lang.String msgContext)
      Asserts a condition.
      private boolean assertFieldNotNull​(java.lang.Object object, java.lang.String field, java.lang.String msgContext)
      Asserts that object in a field is not null.
      private boolean assertJavaIdentifier​(java.lang.String name, java.lang.String field, java.lang.String msgContext)
      Asserts that name in a field is not null, not empty and a Java identifier.
      private <O> boolean assertList​(java.util.List<O> list, boolean fieldNotNull, java.lang.String field, java.lang.String msgContext, Validator.ValidatorFunction<O> func)
      Asserts a list of objects that can be validated.
      private boolean assertStringList​(java.util.List<java.lang.String> list, java.lang.String field, java.lang.String entry, java.lang.String msgContext)
      Asserts that the given string list is either empty or strings are not empty.
      private boolean assertStringNotEmpty​(java.lang.String string, java.lang.String field, java.lang.String msgContext)
      Asserts that string is not empty, in particular not null.
      private boolean assertType​(java.lang.String type, java.util.Map<java.lang.String,​Type> types, java.lang.String field, java.lang.String msgContext)
      Asserts that type is a known type, either in primitives, types or as a Java type.
      private boolean assertVersion​(java.lang.Object object, java.lang.String field, java.lang.String msgContext)
      Asserts that object in a field is a version.
      void clear()
      Clears this instance for re-use.
      java.lang.String getMessages()
      Returns all messages as string.
      boolean hasMessages()
      Returns whether there are validation (error) messages.
      java.lang.Iterable<java.lang.String> messages()
      Returns the validation messages.
      static java.util.Map<java.lang.String,​Type> toMap​(java.util.List<? extends Type> types)
      Turns the given types into a map, mapping the type name to its type descriptor.
      void validate​(Artifact artifact)
      Validates the given artifact (and contained descriptor elements).
      void validate​(Endpoint endpoint)
      Validates the given endpoint (and contained descriptor elements).
      private void validate​(Endpoint endpoint, java.lang.String msgContext)
      Validates the given endpoint (and contained descriptor elements).
      void validate​(ProcessSpec process)
      Validates the given process (and contained descriptor elements).
      private void validate​(ProcessSpec process, java.lang.String msgContext)
      Validates the given process (and contained descriptor elements).
      void validate​(Relation relation, java.util.Map<java.lang.String,​Type> types)
      Validates the given relation (and contained descriptor elements).
      private void validate​(Relation relation, java.util.Map<java.lang.String,​Type> types, java.lang.String msgContext)
      Validates the given relation (and contained descriptor elements).
      void validate​(Service service, java.util.List<? extends Type> types)
      Validates the given service (and contained descriptor elements).
      private void validate​(Service service, java.util.Map<java.lang.String,​Type> types, java.lang.String msgContext)
      Validates the given service (and contained descriptor elements).
      private void validate​(TypedData typed, java.util.Map<java.lang.String,​Type> types, java.lang.String msgContext)
      Validates the given typed data (and contained descriptor elements).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • messages

        private java.util.List<java.lang.String> messages
      • serviceIds

        private java.util.Set<java.lang.String> serviceIds
    • Constructor Detail

      • Validator

        public Validator()
    • Method Detail

      • hasMessages

        public boolean hasMessages()
        Returns whether there are validation (error) messages.
        Returns:
        true for messages, false for no messages/everything is ok
      • getMessages

        public java.lang.String getMessages()
        Returns all messages as string.
        Returns:
        all messages
      • messages

        public java.lang.Iterable<java.lang.String> messages()
        Returns the validation messages.
        Returns:
        the individual messages
      • clear

        public void clear()
        Clears this instance for re-use.
      • validate

        public void validate​(Artifact artifact)
        Validates the given artifact (and contained descriptor elements).
        Parameters:
        artifact - the artifact to validate
      • validate

        public void validate​(Service service,
                             java.util.List<? extends Type> types)
        Validates the given service (and contained descriptor elements).
        Parameters:
        service - the service to validate
        types - the declared types
      • validate

        private void validate​(Service service,
                              java.util.Map<java.lang.String,​Type> types,
                              java.lang.String msgContext)
        Validates the given service (and contained descriptor elements).
        Parameters:
        service - the service to validate
        types - the declared types
        msgContext - nested context information for location of unnamed elements in validation messages
      • toMap

        public static java.util.Map<java.lang.String,​Type> toMap​(java.util.List<? extends Type> types)
        Turns the given types into a map, mapping the type name to its type descriptor.
        Parameters:
        types - the types to map
        Returns:
        the mapped types
      • validate

        public void validate​(Relation relation,
                             java.util.Map<java.lang.String,​Type> types)
        Validates the given relation (and contained descriptor elements).
        Parameters:
        relation - the relation to validate
        types - the declared types
      • validate

        private void validate​(TypedData typed,
                              java.util.Map<java.lang.String,​Type> types,
                              java.lang.String msgContext)
        Validates the given typed data (and contained descriptor elements).
        Parameters:
        typed - the typed data to validate
        msgContext - nested context information for location of unnamed elements in validation messages
        types - the declared types
      • validate

        private void validate​(Relation relation,
                              java.util.Map<java.lang.String,​Type> types,
                              java.lang.String msgContext)
        Validates the given relation (and contained descriptor elements).
        Parameters:
        relation - the relation to validate
        msgContext - nested context information for location of unnamed elements in validation messages
        types - the declared types
      • validate

        public void validate​(ProcessSpec process)
        Validates the given process (and contained descriptor elements).
        Parameters:
        process - the process to validate
      • validate

        private void validate​(ProcessSpec process,
                              java.lang.String msgContext)
        Validates the given process (and contained descriptor elements).
        Parameters:
        process - the process to validate
        msgContext - nested context information for location of unnamed elements in validation messages
      • appendToContext

        private java.lang.String appendToContext​(java.lang.String context,
                                                 java.lang.String text)
        Appends text to context considering that context may be empty or null.
        Parameters:
        context - the context to append, may be empty or null
        text - the text to append
        Returns:
        the appended context information
      • validate

        public void validate​(Endpoint endpoint)
        Validates the given endpoint (and contained descriptor elements).
        Parameters:
        endpoint - the endpoint to validate
      • validate

        private void validate​(Endpoint endpoint,
                              java.lang.String msgContext)
        Validates the given endpoint (and contained descriptor elements).
        Parameters:
        endpoint - the endpoint to validate
        msgContext - nested context information for location of unnamed elements in validation messages
      • assertType

        private boolean assertType​(java.lang.String type,
                                   java.util.Map<java.lang.String,​Type> types,
                                   java.lang.String field,
                                   java.lang.String msgContext)
        Asserts that type is a known type, either in primitives, types or as a Java type.
        Parameters:
        type - the type name to check
        types - the declared types
        field - the field the string is taken from for composing an error message
        msgContext - the context of the message/validate element for better location by the caller, ignored if empty or null
        Returns:
        true if successful, false if failed
      • assertJavaIdentifier

        private boolean assertJavaIdentifier​(java.lang.String name,
                                             java.lang.String field,
                                             java.lang.String msgContext)
        Asserts that name in a field is not null, not empty and a Java identifier.
        Parameters:
        name - the name to check
        field - the field the string is taken from for composing an error message
        msgContext - the context of the message/validate element for better location by the caller, ignored if empty or null
        Returns:
        true if successful, false if failed
      • assertFieldNotNull

        private boolean assertFieldNotNull​(java.lang.Object object,
                                           java.lang.String field,
                                           java.lang.String msgContext)
        Asserts that object in a field is not null.
        Parameters:
        object - the object to check
        field - the field the string is taken from for composing an error message
        msgContext - the context of the message/validate element for better location by the caller, ignored if empty or null
        Returns:
        true if successful, false if failed
      • assertVersion

        private boolean assertVersion​(java.lang.Object object,
                                      java.lang.String field,
                                      java.lang.String msgContext)
        Asserts that object in a field is a version.
        Parameters:
        object - the object to check
        field - the field the string is taken from for composing an error message
        msgContext - the context of the message/validate element for better location by the caller, ignored if empty or null
        Returns:
        true if successful, false if failed
      • assertList

        private <O> boolean assertList​(java.util.List<O> list,
                                       boolean fieldNotNull,
                                       java.lang.String field,
                                       java.lang.String msgContext,
                                       Validator.ValidatorFunction<O> func)
        Asserts a list of objects that can be validated.
        Type Parameters:
        O - the type of objects
        Parameters:
        list - the list of objects to be asserted
        fieldNotNull - if true, apply assertFieldNotNull(Object, String, String) to list and return its result, else return always true
        field - the field the string is taken from for composing an error message
        msgContext - the context of the message/validate element for better location by the caller, ignored if empty or null
        func - if list as object/field is considered to be ok, apply func to each element with respective iterative context set
        Returns:
        true if successful, false if failed
      • assertStringList

        private boolean assertStringList​(java.util.List<java.lang.String> list,
                                         java.lang.String field,
                                         java.lang.String entry,
                                         java.lang.String msgContext)
        Asserts that the given string list is either empty or strings are not empty.
        Parameters:
        list - the command line argument list
        field - the field the string is taken from for composing an error message
        entry - the symbolic name of an entry for composing an error message
        msgContext - the context of the message/validate element for better location by the caller, ignored if empty or null
        Returns:
        true if successful, false if failed
      • assertStringNotEmpty

        private boolean assertStringNotEmpty​(java.lang.String string,
                                             java.lang.String field,
                                             java.lang.String msgContext)
        Asserts that string is not empty, in particular not null.
        Parameters:
        string - the string to check
        field - the field the string is taken from for composing an error message
        msgContext - the context of the message/validate element for better location by the caller, ignored if empty or null
        Returns:
        true if successful, false if failed
      • assertCondition

        private boolean assertCondition​(boolean condition,
                                        java.lang.String msg,
                                        java.lang.String msgContext)
        Asserts a condition.
        Parameters:
        condition - the evaluated condition
        msg - the message if condition does not hold. A missing trailing "." is added.
        msgContext - the context of the message/validate element for better location by the caller, ignored if empty or null
        Returns:
        true if successful, false if failed
      • appendContext

        private java.lang.String appendContext​(java.lang.String msg,
                                               java.lang.String msgContext)
        Appends the msgContext to msg for output.
        Parameters:
        msg - the message
        msgContext - the message context
        Returns:
        msg with appende context