3737@ Component
3838public class FileStorage {
3939
40- private final Pattern creationTimePattern = Pattern .compile ("creationTime=\" (.+ )\" " );
40+ private final Pattern creationTimePattern = Pattern .compile ("creationTime=\" (\\ d{4}- \\ d{2}- \\ d{2} \\ d{2}: \\ d{2}: \\ d{2} )\" " );
4141
42- private final Pattern previousCreationTimePattern = Pattern .compile ("previousCreationTime=\" (.+ )\" " );
42+ private final Pattern previousCreationTimePattern = Pattern .compile ("previousCreationTime=\" (\\ d{4}- \\ d{2}- \\ d{2} \\ d{2}: \\ d{2}: \\ d{2} )\" " );
4343
4444 /**
4545 * The Conf.
@@ -100,16 +100,9 @@ public void saveReport(NamedReport report, CurationEntityType entityType, boolea
100100 }
101101 }
102102
103- Path xmlFilePath = saveReportAsXML (report , entityType );
104-
105- Path htmlFilePath = saveReportAsHTML (report , entityType );
106-
107-
108- if (makeStampedCopy ) {
109- copyStampedFile (xmlFilePath , report );
110- copyStampedFile (htmlFilePath , report );
111- }
103+ saveReportAsXML (report , entityType , makeStampedCopy );
112104
105+ saveReportAsHTML (report , entityType , makeStampedCopy );
113106 }
114107
115108 /**
@@ -134,7 +127,7 @@ public void checkOutputPath() {
134127 * @param entityType the entity type
135128 * @return the path
136129 */
137- public Path saveReportAsXML (NamedReport report , CurationEntityType entityType ) {
130+ public Path saveReportAsXML (NamedReport report , CurationEntityType entityType , boolean makeStampedCopy ) {
138131
139132 Path outputPath = getOutputPath (report .getName (), entityType , "xml" );
140133
@@ -172,6 +165,22 @@ public Path saveReportAsXML(NamedReport report, CurationEntityType entityType) {
172165 log .error ("can't marshall report '{}'" , report .getName ());
173166 }
174167
168+ if (makeStampedCopy ){
169+
170+ String stampedFileName = report .getName () + "_" + report .getCreationTime ().format (DateTimeFormatter .ISO_LOCAL_DATE ) + ".xml" ;
171+
172+ Path stampedPath = outputPath .getParent ().resolve (stampedFileName );
173+
174+ try {
175+ Files .copy (outputPath , stampedPath , StandardCopyOption .REPLACE_EXISTING );
176+ }
177+ catch (IOException e ) {
178+
179+ log .error ("can't copy file '{}' to stamped file '{}'" , outputPath , stampedPath );
180+ throw new RuntimeException (e );
181+ }
182+ }
183+
175184 return outputPath ;
176185 }
177186
@@ -182,7 +191,7 @@ public Path saveReportAsXML(NamedReport report, CurationEntityType entityType) {
182191 * @param entityType the entity type
183192 * @return the path
184193 */
185- public Path saveReportAsHTML (NamedReport report , CurationEntityType entityType ) {
194+ public Path saveReportAsHTML (NamedReport report , CurationEntityType entityType , boolean makeStampedCopy ) {
186195
187196 Path outputPath = getOutputPath (report .getName (), entityType , "html" );
188197
@@ -213,6 +222,51 @@ public Path saveReportAsHTML(NamedReport report, CurationEntityType entityType)
213222
214223 }
215224
225+ if (makeStampedCopy ){
226+
227+ String stampedFileName = report .getName () + "_" + report .getCreationTime ().format (DateTimeFormatter .ISO_LOCAL_DATE ) + ".html" ;
228+
229+ Path stampedPath = outputPath .getParent ().resolve (stampedFileName );
230+
231+ resourceName = "/xslt/" + report .getClass ().getSimpleName () + "2HTMLStamped.xsl" ;
232+
233+ InputStream in = this .getClass ().getResourceAsStream (resourceName );
234+
235+ if (in != null ){
236+
237+ xslt = new StreamSource (in );
238+
239+ try {
240+ transformer = factory .newTransformer (xslt );
241+
242+ transformer .transform (new JAXBSource (JAXBContext .newInstance (report .getClass ()), report ),
243+ new StreamResult (stampedPath .toFile ()));
244+ }
245+ catch (TransformerConfigurationException e ) {
246+
247+ log .error ("can't load resource '{}'" , resourceName );
248+ throw new RuntimeException (e );
249+
250+ }
251+ catch (TransformerException | JAXBException e ) {
252+
253+ log .error ("can't transform report '{}'" , report .getName ());
254+
255+ }
256+ }
257+ else {
258+
259+ try {
260+ Files .copy (outputPath , stampedPath , StandardCopyOption .REPLACE_EXISTING );
261+ }
262+ catch (IOException e ) {
263+
264+ log .error ("can't copy file '{}' to stamped file '{}'" , outputPath , stampedPath );
265+ throw new RuntimeException (e );
266+ }
267+ }
268+ }
269+
216270 return outputPath ;
217271 }
218272
@@ -235,19 +289,4 @@ private Path getOutputPath(String reportName, CurationEntityType entityType, Str
235289
236290 return outPath .resolve (filename );
237291 }
238-
239- private void copyStampedFile (Path path , NamedReport report ) {
240- String stampedFileName = path .getFileName ().toString ().replace ("." ,
241- "_" + report .getCreationTime ().format (DateTimeFormatter .ISO_LOCAL_DATE ) + "." );
242- Path stampedPath = path .getParent ().resolve (stampedFileName );
243- try {
244- Files .copy (path , stampedPath , StandardCopyOption .REPLACE_EXISTING );
245- }
246- catch (IOException e ) {
247-
248- log .error ("can't copy file '{}' to stamped file '{}'" , path , stampedPath );
249- throw new RuntimeException (e );
250- }
251- }
252-
253292}
0 commit comments