@@ -109,65 +109,68 @@ public void validateWarning(String filePath, String expectedWarning) {
109
109
* @param filePath file to validate
110
110
* @return error message if validation fails, null is validation succeeds
111
111
*/
112
- public synchronized String validate (String filePath ) {
112
+ public String validate (String filePath ) {
113
113
// VeraPdf doesn't work in native mode so skip VeraPdf validation
114
114
if (isNative ) {
115
115
return null ;
116
116
}
117
117
118
- String errorMessage = null ;
119
- try {
120
- File xmlReport = new File (filePath .substring (0 , filePath .length () - ".pdf" .length ()) + ".xml" );
121
- VeraGreenfieldFoundryProvider .initialise ();
122
-
123
- // Initializes default VeraPDF configurations
124
- ProcessorConfig customProfile = ProcessorFactory .defaultConfig ();
125
- FeatureExtractorConfig featuresConfig = customProfile .getFeatureConfig ();
126
- ValidatorConfig valConfig = ValidatorFactory .createConfig (getSpecification (), false , -1 , false , true ,
127
- Level .WARNING , "" , false );
128
- PluginsCollectionConfig plugConfig = customProfile .getPluginsCollectionConfig ();
129
- MetadataFixerConfig metaConfig = customProfile .getFixerConfig ();
130
- ProcessorConfig resultConfig = ProcessorFactory .fromValues (valConfig , featuresConfig ,
131
- plugConfig , metaConfig , EnumSet .of (TaskType .VALIDATE ));
132
-
133
- // Creates validation processor
134
- BatchProcessor processor = ProcessorFactory .fileBatchProcessor (resultConfig );
135
-
136
- BatchSummary summary = processor .process (Collections .singletonList (new File (filePath )),
137
- ProcessorFactory .getHandler (FormatOption .XML , true ,
138
- Files .newOutputStream (Paths .get (String .valueOf (xmlReport ))), false ));
139
-
140
- LogsSummary logsSummary = LogsSummaryImpl .getSummary ();
141
- String xmlReportPath = "file://" + xmlReport .toURI ().normalize ().getPath ();
142
-
143
- if (summary .getFailedParsingJobs () != 0 ) {
144
- errorMessage = "An error occurred while parsing current file. See report: " + xmlReportPath ;
145
- } else if (summary .getFailedEncryptedJobs () != 0 ) {
146
- errorMessage = "VeraPDF execution failed - specified file is encrypted. See report: " + xmlReportPath ;
147
- } else if (summary .getValidationSummary ().getNonCompliantPdfaCount () != 0 ) {
148
- errorMessage = "VeraPDF verification failed. See verification results: " + xmlReportPath ;
149
- } else {
150
- if (logToConsole ) {
151
- System .out .println ("VeraPDF verification finished. See verification report: " + xmlReportPath );
152
- }
153
-
154
- if (logsSummary .getLogsCount () != 0 ) {
155
- errorMessage = "The following warnings and errors were logged during validation:" ;
156
- errorMessage += logsSummary .getLogs ().stream ()
157
- .map (log -> "\n " + log .getLevel () + ": " + log .getMessage ())
158
- .sorted ()
159
- .collect (Collectors .joining ());
118
+ synchronized (VeraPdfValidator .class ) {
119
+ String errorMessage = null ;
120
+ try {
121
+ File xmlReport = new File (filePath .substring (0 , filePath .length () - ".pdf" .length ()) + ".xml" );
122
+ VeraGreenfieldFoundryProvider .initialise ();
123
+
124
+ // Initializes default VeraPDF configurations
125
+ ProcessorConfig customProfile = ProcessorFactory .defaultConfig ();
126
+ FeatureExtractorConfig featuresConfig = customProfile .getFeatureConfig ();
127
+ ValidatorConfig valConfig = ValidatorFactory .createConfig (getSpecification (), false , -1 , false , true ,
128
+ Level .WARNING , "" , false );
129
+ PluginsCollectionConfig plugConfig = customProfile .getPluginsCollectionConfig ();
130
+ MetadataFixerConfig metaConfig = customProfile .getFixerConfig ();
131
+ ProcessorConfig resultConfig = ProcessorFactory .fromValues (valConfig , featuresConfig ,
132
+ plugConfig , metaConfig , EnumSet .of (TaskType .VALIDATE ));
133
+
134
+ // Creates validation processor
135
+ BatchProcessor processor = ProcessorFactory .fileBatchProcessor (resultConfig );
136
+
137
+ BatchSummary summary = processor .process (Collections .singletonList (new File (filePath )),
138
+ ProcessorFactory .getHandler (FormatOption .XML , true ,
139
+ Files .newOutputStream (Paths .get (String .valueOf (xmlReport ))), false ));
140
+
141
+ LogsSummary logsSummary = LogsSummaryImpl .getSummary ();
142
+ String xmlReportPath = "file://" + xmlReport .toURI ().normalize ().getPath ();
143
+
144
+ if (summary .getFailedParsingJobs () != 0 ) {
145
+ errorMessage = "An error occurred while parsing current file. See report: " + xmlReportPath ;
146
+ } else if (summary .getFailedEncryptedJobs () != 0 ) {
147
+ errorMessage =
148
+ "VeraPDF execution failed - specified file is encrypted. See report: " + xmlReportPath ;
149
+ } else if (summary .getValidationSummary ().getNonCompliantPdfaCount () != 0 ) {
150
+ errorMessage = "VeraPDF verification failed. See verification results: " + xmlReportPath ;
151
+ } else {
152
+ if (logToConsole ) {
153
+ System .out .println ("VeraPDF verification finished. See verification report: " + xmlReportPath );
154
+ }
155
+
156
+ if (logsSummary .getLogsCount () != 0 ) {
157
+ errorMessage = "The following warnings and errors were logged during validation:" ;
158
+ errorMessage += logsSummary .getLogs ().stream ()
159
+ .map (log -> "\n " + log .getLevel () + ": " + log .getMessage ())
160
+ .sorted ()
161
+ .collect (Collectors .joining ());
162
+ }
160
163
}
164
+ } catch (IOException | VeraPDFException exc ) {
165
+ errorMessage = "VeraPDF execution failed:\n " + exc .getMessage ();
161
166
}
162
- } catch (IOException | VeraPDFException exc ) {
163
- errorMessage = "VeraPDF execution failed:\n " + exc .getMessage ();
164
- }
165
167
166
- if (errorMessage != null ) {
167
- if (logToConsole ) {
168
- System .out .println (errorMessage );
168
+ if (errorMessage != null ) {
169
+ if (logToConsole ) {
170
+ System .out .println (errorMessage );
171
+ }
169
172
}
173
+ return errorMessage ;
170
174
}
171
- return errorMessage ;
172
175
}
173
176
}
0 commit comments