|
| 1 | +package org.fugerit.java.doc.val.core; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +import org.fugerit.java.core.cfg.ConfigRuntimeException; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +public class DocValidatorFacade { |
| 18 | + |
| 19 | + private static final Logger logger = LoggerFactory.getLogger( DocValidatorFacade.class ); |
| 20 | + |
| 21 | + private List<DocTypeValidator> validators; |
| 22 | + |
| 23 | + private Map<String, DocTypeValidator> extMapValidator; |
| 24 | + |
| 25 | + private Map<String, DocTypeValidator> mimMapValidator; |
| 26 | + |
| 27 | + private DocValidatorFacade() { |
| 28 | + this.validators = new ArrayList<>(); |
| 29 | + this.extMapValidator = new HashMap<>(); |
| 30 | + this.mimMapValidator = new HashMap<>(); |
| 31 | + } |
| 32 | + |
| 33 | + private boolean addValidator( DocTypeValidator validator ) { |
| 34 | + boolean ok = true; |
| 35 | + this.validators.add(validator); |
| 36 | + DocTypeValidator previous = this.mimMapValidator.put( validator.getMimeType(), validator ); |
| 37 | + if ( previous != null ) { |
| 38 | + ok = false; |
| 39 | + logger.warn( "Validator {} has been overridden for mimeType {}", previous, validator.getMimeType() ); |
| 40 | + } |
| 41 | + for ( String ext : validator.getSupportedExtensions() ) { |
| 42 | + previous = this.extMapValidator.put(ext, validator); |
| 43 | + if ( previous != null ) { |
| 44 | + ok = false; |
| 45 | + logger.warn( "Validator {} has been overridden for extension {}", previous, ext ); |
| 46 | + } |
| 47 | + } |
| 48 | + return ok; |
| 49 | + } |
| 50 | + |
| 51 | + public List<DocTypeValidator> findAllByMimeType( final String mimeType ) { |
| 52 | + return this.validators.stream().filter( v -> v.isMimeTypeSupported( mimeType ) ).collect( Collectors.toList() ); |
| 53 | + } |
| 54 | + |
| 55 | + public List<DocTypeValidator> findAllExtension( final String extension ) { |
| 56 | + return this.validators.stream().filter( v -> v.isExtensionSupported( extension ) ).collect( Collectors.toList() ); |
| 57 | + } |
| 58 | + |
| 59 | + public DocTypeValidator findByMimeType( String mimeType ) { |
| 60 | + return this.mimMapValidator.get(mimeType); |
| 61 | + } |
| 62 | + |
| 63 | + public DocTypeValidator findByExtension( String extension ) { |
| 64 | + return this.extMapValidator.get( extension ); |
| 65 | + } |
| 66 | + |
| 67 | + public boolean isMimeTypeSupprted( String mimeType ) { |
| 68 | + DocTypeValidator validator = this.findByMimeType(mimeType); |
| 69 | + return validator != null && validator.isMimeTypeSupported(mimeType); |
| 70 | + } |
| 71 | + |
| 72 | + public boolean isExtensionSupported( String extension ) { |
| 73 | + DocTypeValidator validator = this.findByExtension(extension); |
| 74 | + return validator != null && validator.isExtensionSupported(extension); |
| 75 | + } |
| 76 | + |
| 77 | + public boolean check( File file ) throws IOException { |
| 78 | + return this.validate(file).isResultOk(); |
| 79 | + } |
| 80 | + |
| 81 | + public DocTypeValidationResult validate( File file ) throws IOException { |
| 82 | + DocTypeValidationResult result = DocTypeValidationResult.newFail(); |
| 83 | + try ( InputStream is = new FileInputStream( file ) ) { |
| 84 | + result = this.validate( file.getName(), is ); |
| 85 | + } |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + public boolean check( String fileName, InputStream is ) throws IOException { |
| 90 | + return this.validate(fileName, is).isResultOk(); |
| 91 | + } |
| 92 | + |
| 93 | + public DocTypeValidationResult validate( String fileName, InputStream is ) throws IOException { |
| 94 | + String extension = fileName.substring( fileName.lastIndexOf( '.' )+1 ); |
| 95 | + return validateByExtension(extension, is); |
| 96 | + } |
| 97 | + |
| 98 | + public boolean checkByExtension( String extension, InputStream is ) throws IOException { |
| 99 | + return this.validateByExtension(extension, is).isResultOk(); |
| 100 | + } |
| 101 | + |
| 102 | + public DocTypeValidationResult validateByExtension( String extension, InputStream is ) throws IOException { |
| 103 | + return this.evaluate(this.findByExtension(extension.toUpperCase()) , is); |
| 104 | + } |
| 105 | + |
| 106 | + public boolean checkByMimeType( String mimeType, InputStream is ) throws IOException { |
| 107 | + return this.validateByMimeType(mimeType, is).isResultOk(); |
| 108 | + } |
| 109 | + |
| 110 | + public DocTypeValidationResult validateByMimeType( String mimeType, InputStream is ) throws IOException { |
| 111 | + return this.evaluate(this.findByMimeType(mimeType) , is); |
| 112 | + } |
| 113 | + |
| 114 | + private DocTypeValidationResult evaluate( DocTypeValidator validator, InputStream is ) throws IOException { |
| 115 | + DocTypeValidationResult result = DocTypeValidationResult.newFail(); |
| 116 | + if ( validator != null ) { |
| 117 | + result = validator.validate( is ); |
| 118 | + } else { |
| 119 | + logger.info( "no validator found!" ); |
| 120 | + } |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + public static DocValidatorFacade newFacade( DocTypeValidator... validators ) { |
| 125 | + DocValidatorFacade facade = new DocValidatorFacade(); |
| 126 | + for ( int k=0; k<validators.length; k++ ) { |
| 127 | + DocTypeValidator v = validators[k]; |
| 128 | + logger.info( "add validator {}", v ); |
| 129 | + facade.addValidator( v ); |
| 130 | + } |
| 131 | + return facade; |
| 132 | + } |
| 133 | + |
| 134 | + public static DocValidatorFacade newFacadeStrict( DocTypeValidator... validators ) { |
| 135 | + DocValidatorFacade facade = new DocValidatorFacade(); |
| 136 | + for ( int k=0; k<validators.length; k++ ) { |
| 137 | + DocTypeValidator v = validators[k]; |
| 138 | + logger.info( "add validator {}", v ); |
| 139 | + boolean ok = facade.addValidator( v ); |
| 140 | + if ( !ok ) { |
| 141 | + throw new ConfigRuntimeException( "Facade creation error for validator : "+v ); |
| 142 | + } |
| 143 | + } |
| 144 | + return facade; |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments