Is there any gradle plugin to validate XML?









up vote
0
down vote

favorite












I work on a project where we have xml files for code generation and we use gradle to build it.



I'm novice in gradle, but I heard that there are a lot of plugins that could help with routine tasks and I wonder if there are some plugins for xml simple validation (missing quotes and brackets).



I would like to get name of file and list of missings as result.



PS Tried to search in google, but couldn't find something like that.



UPD If in not so distant future full validation of xml file (tags, parameters) would be required, what should I do?










share|improve this question























  • The term "validation" in the XML world has a specialized meaning: it means checking that the XML conforms with some supplied schema. A schema might say that a p element can contain i elements but not vice versa. I suspect this isn't what you are looking for: the phrase "missing quotes and brackets" suggests you just want to check if a file contains well-formed XML.
    – Michael Kay
    Nov 10 at 11:26














up vote
0
down vote

favorite












I work on a project where we have xml files for code generation and we use gradle to build it.



I'm novice in gradle, but I heard that there are a lot of plugins that could help with routine tasks and I wonder if there are some plugins for xml simple validation (missing quotes and brackets).



I would like to get name of file and list of missings as result.



PS Tried to search in google, but couldn't find something like that.



UPD If in not so distant future full validation of xml file (tags, parameters) would be required, what should I do?










share|improve this question























  • The term "validation" in the XML world has a specialized meaning: it means checking that the XML conforms with some supplied schema. A schema might say that a p element can contain i elements but not vice versa. I suspect this isn't what you are looking for: the phrase "missing quotes and brackets" suggests you just want to check if a file contains well-formed XML.
    – Michael Kay
    Nov 10 at 11:26












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I work on a project where we have xml files for code generation and we use gradle to build it.



I'm novice in gradle, but I heard that there are a lot of plugins that could help with routine tasks and I wonder if there are some plugins for xml simple validation (missing quotes and brackets).



I would like to get name of file and list of missings as result.



PS Tried to search in google, but couldn't find something like that.



UPD If in not so distant future full validation of xml file (tags, parameters) would be required, what should I do?










share|improve this question















I work on a project where we have xml files for code generation and we use gradle to build it.



I'm novice in gradle, but I heard that there are a lot of plugins that could help with routine tasks and I wonder if there are some plugins for xml simple validation (missing quotes and brackets).



I would like to get name of file and list of missings as result.



PS Tried to search in google, but couldn't find something like that.



UPD If in not so distant future full validation of xml file (tags, parameters) would be required, what should I do?







xml validation gradle automation gradle-plugin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 20:08

























asked Nov 9 at 18:02









Denis Sablukov

550519




550519











  • The term "validation" in the XML world has a specialized meaning: it means checking that the XML conforms with some supplied schema. A schema might say that a p element can contain i elements but not vice versa. I suspect this isn't what you are looking for: the phrase "missing quotes and brackets" suggests you just want to check if a file contains well-formed XML.
    – Michael Kay
    Nov 10 at 11:26
















  • The term "validation" in the XML world has a specialized meaning: it means checking that the XML conforms with some supplied schema. A schema might say that a p element can contain i elements but not vice versa. I suspect this isn't what you are looking for: the phrase "missing quotes and brackets" suggests you just want to check if a file contains well-formed XML.
    – Michael Kay
    Nov 10 at 11:26















The term "validation" in the XML world has a specialized meaning: it means checking that the XML conforms with some supplied schema. A schema might say that a p element can contain i elements but not vice versa. I suspect this isn't what you are looking for: the phrase "missing quotes and brackets" suggests you just want to check if a file contains well-formed XML.
– Michael Kay
Nov 10 at 11:26




The term "validation" in the XML world has a specialized meaning: it means checking that the XML conforms with some supplied schema. A schema might say that a p element can contain i elements but not vice versa. I suspect this isn't what you are looking for: the phrase "missing quotes and brackets" suggests you just want to check if a file contains well-formed XML.
– Michael Kay
Nov 10 at 11:26












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










It's pretty simple to write your own



class XmlValidate extends DefaultTask 
@InputFiles
private FileCollection xmlFiles

@InputFile
File xsd

void xml(Object files)
FileCollection fc = project.files(files)
this.xmlFiles = this.xmlFiles == null ? fc : this.xmlFiles.add(fc)


@TaskAction
public void validateXml()
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder()
Validator validator = null
if (xsd != null)
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
Schema schema = factory.newSchema(new StreamSource(xsd))
validator = schema.newValidator()

Set<File> failures = as Set
xmlFiles.forEach
Document document = null
try
document = parser.parse(it)
catch (Exception e)
logger.error("Error parsing $it", e)
failures << it

if (document && validator)
try
validator.validate(new DOMSource(document))
catch (Exception e)
logger.error("Error validating $it", e)
failures << it



if (failures) throw new BuildException("xml validation failures $failures")




Usage in build.gradle



task validateXml(type: XmlValidate) 
xml ['foo.xml', 'bar.xml']
xml fileTree(dir: 'src/main/resources/baz', include: '*.xml')
xsd = file('path/to.xsd')






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231114%2fis-there-any-gradle-plugin-to-validate-xml%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    It's pretty simple to write your own



    class XmlValidate extends DefaultTask 
    @InputFiles
    private FileCollection xmlFiles

    @InputFile
    File xsd

    void xml(Object files)
    FileCollection fc = project.files(files)
    this.xmlFiles = this.xmlFiles == null ? fc : this.xmlFiles.add(fc)


    @TaskAction
    public void validateXml()
    DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder()
    Validator validator = null
    if (xsd != null)
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
    Schema schema = factory.newSchema(new StreamSource(xsd))
    validator = schema.newValidator()

    Set<File> failures = as Set
    xmlFiles.forEach
    Document document = null
    try
    document = parser.parse(it)
    catch (Exception e)
    logger.error("Error parsing $it", e)
    failures << it

    if (document && validator)
    try
    validator.validate(new DOMSource(document))
    catch (Exception e)
    logger.error("Error validating $it", e)
    failures << it



    if (failures) throw new BuildException("xml validation failures $failures")




    Usage in build.gradle



    task validateXml(type: XmlValidate) 
    xml ['foo.xml', 'bar.xml']
    xml fileTree(dir: 'src/main/resources/baz', include: '*.xml')
    xsd = file('path/to.xsd')






    share|improve this answer


























      up vote
      1
      down vote



      accepted










      It's pretty simple to write your own



      class XmlValidate extends DefaultTask 
      @InputFiles
      private FileCollection xmlFiles

      @InputFile
      File xsd

      void xml(Object files)
      FileCollection fc = project.files(files)
      this.xmlFiles = this.xmlFiles == null ? fc : this.xmlFiles.add(fc)


      @TaskAction
      public void validateXml()
      DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder()
      Validator validator = null
      if (xsd != null)
      SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
      Schema schema = factory.newSchema(new StreamSource(xsd))
      validator = schema.newValidator()

      Set<File> failures = as Set
      xmlFiles.forEach
      Document document = null
      try
      document = parser.parse(it)
      catch (Exception e)
      logger.error("Error parsing $it", e)
      failures << it

      if (document && validator)
      try
      validator.validate(new DOMSource(document))
      catch (Exception e)
      logger.error("Error validating $it", e)
      failures << it



      if (failures) throw new BuildException("xml validation failures $failures")




      Usage in build.gradle



      task validateXml(type: XmlValidate) 
      xml ['foo.xml', 'bar.xml']
      xml fileTree(dir: 'src/main/resources/baz', include: '*.xml')
      xsd = file('path/to.xsd')






      share|improve this answer
























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        It's pretty simple to write your own



        class XmlValidate extends DefaultTask 
        @InputFiles
        private FileCollection xmlFiles

        @InputFile
        File xsd

        void xml(Object files)
        FileCollection fc = project.files(files)
        this.xmlFiles = this.xmlFiles == null ? fc : this.xmlFiles.add(fc)


        @TaskAction
        public void validateXml()
        DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder()
        Validator validator = null
        if (xsd != null)
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
        Schema schema = factory.newSchema(new StreamSource(xsd))
        validator = schema.newValidator()

        Set<File> failures = as Set
        xmlFiles.forEach
        Document document = null
        try
        document = parser.parse(it)
        catch (Exception e)
        logger.error("Error parsing $it", e)
        failures << it

        if (document && validator)
        try
        validator.validate(new DOMSource(document))
        catch (Exception e)
        logger.error("Error validating $it", e)
        failures << it



        if (failures) throw new BuildException("xml validation failures $failures")




        Usage in build.gradle



        task validateXml(type: XmlValidate) 
        xml ['foo.xml', 'bar.xml']
        xml fileTree(dir: 'src/main/resources/baz', include: '*.xml')
        xsd = file('path/to.xsd')






        share|improve this answer














        It's pretty simple to write your own



        class XmlValidate extends DefaultTask 
        @InputFiles
        private FileCollection xmlFiles

        @InputFile
        File xsd

        void xml(Object files)
        FileCollection fc = project.files(files)
        this.xmlFiles = this.xmlFiles == null ? fc : this.xmlFiles.add(fc)


        @TaskAction
        public void validateXml()
        DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder()
        Validator validator = null
        if (xsd != null)
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
        Schema schema = factory.newSchema(new StreamSource(xsd))
        validator = schema.newValidator()

        Set<File> failures = as Set
        xmlFiles.forEach
        Document document = null
        try
        document = parser.parse(it)
        catch (Exception e)
        logger.error("Error parsing $it", e)
        failures << it

        if (document && validator)
        try
        validator.validate(new DOMSource(document))
        catch (Exception e)
        logger.error("Error validating $it", e)
        failures << it



        if (failures) throw new BuildException("xml validation failures $failures")




        Usage in build.gradle



        task validateXml(type: XmlValidate) 
        xml ['foo.xml', 'bar.xml']
        xml fileTree(dir: 'src/main/resources/baz', include: '*.xml')
        xsd = file('path/to.xsd')







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 13:23

























        answered Nov 10 at 12:14









        lance-java

        15.9k12858




        15.9k12858



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231114%2fis-there-any-gradle-plugin-to-validate-xml%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo