/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2015 wcm.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.wcm.devops.conga.plugins.aem.validator;

import io.wcm.devops.conga.generator.spi.ValidationException;
import io.wcm.devops.conga.generator.spi.ValidatorPlugin;
import io.wcm.devops.conga.generator.spi.context.FileContext;
import io.wcm.devops.conga.generator.spi.context.ValidatorContext;
import io.wcm.devops.conga.generator.util.FileUtil;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.xml.sax.InputSource;

import com.day.any.BaseHandler;
import com.day.any.Parser;

/**
 * Validates Day ANY files.
 */
public class AnyValidator implements ValidatorPlugin {

  /**
   * Plugin name
   */
  public static final String NAME = "any";

  private static final String FILE_EXTENSION = "any";

  @Override
  public String getName() {
    return NAME;
  }

  @Override
  public boolean accepts(FileContext file, ValidatorContext context) {
    return FileUtil.matchesExtension(file, FILE_EXTENSION);
  }

  @Override
  public Void apply(FileContext file, ValidatorContext context) throws ValidationException {
    Parser parser = new Parser(new BaseHandler());
    try (InputStream is = new FileInputStream(file.getFile());
        Reader reader = new InputStreamReader(is, file.getCharset())) {
      parser.parse(new InputSource(reader));
    }
    catch (Throwable ex) {
      throw new ValidationException("ANY file is not valid: " + ex.getMessage(), ex);
    }
    return null;
  }

}

@@ -999,32 +999,60 @@
 ileInputStream;%0A
+import java.io.IOException;%0A
 import java.io.I
@@ -1140,27 +1140,95 @@
 xml.sax.
-InputSource
+EntityResolver;%0Aimport org.xml.sax.InputSource;%0Aimport org.xml.sax.SAXException
 ;%0A%0Aimpor
@@ -1280,16 +1280,53 @@
 .Parser;
+%0Aimport com.day.any.ResourceExpander;
 %0A%0A/**%0A *
@@ -1901,24 +1901,577 @@
 Handler());%0A
+%0A    // set resource expander and entity resolver that do not resolve anything%0A    // just make sure they are in place to allow any parser parsing files with include directives%0A    parser.setResourceExpander(new ResourceExpander() %7B%0A      @Override%0A      public String%5B%5D expand(String arg0) %7B%0A        return new String%5B0%5D;%0A      %7D%0A    %7D);%0A    parser.setEnitiyResolver(new EntityResolver() %7B%0A      @Override%0A      public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException %7B%0A        return null;%0A      %7D%0A    %7D);%0A%0A
     try (Inp

set resource expander and entity resolver that do not resolve anything just make sure they are in place to allow any parser parsing files with include directives