publicclassFindPropertiesByRegex{publicstaticvoidrun(){Patternpattern=Pattern.compile("author|company",Pattern.CASE_INSENSITIVE);try(Metadatametadata=newMetadata(Constants.InputDocx)){// This method searches for properties across all metadata packages and works with all supported formatsIReadOnlyList<MetadataProperty>properties=metadata.findProperties(newFindPropertiesByRegex().newRegexSpecification(pattern));for(MetadataPropertyproperty:properties){System.out.println(String.format("%s = %s",property.getName(),property.getValue()));}}}publicclassRegexSpecificationextendsSpecification{privatePatternpattern;publicRegexSpecification(Patternpattern){this.pattern=pattern;}@OverridepublicbooleanisSatisfiedBy(MetadataPropertymetadataProperty){Matchermatcher=pattern.matcher(metadataProperty.getName());returnmatcher.find();}}}
Replacing Metadata using Regular Expressions
The following code snippets show how to replace metadata properties using a regular expression.
publicclassUpdatePropertiesByRegex{publicstaticvoidrun(){Patternpattern=Pattern.compile("^author|company$",Pattern.CASE_INSENSITIVE);PropertyValuereplaceValue=newPropertyValue("Aspose");try(Metadatametadata=newMetadata(Constants.InputDocx)){// This method updates writable properties across all metadata packages and works with all supported formatsmetadata.updateProperties(newUpdatePropertiesByRegex().newRegexSpecification(pattern),replaceValue);metadata.save(Constants.OutputDocx);}}privateclassRegexSpecificationextendsSpecification{privatePatternpattern;publicRegexSpecification(Patternpattern){this.pattern=pattern;}@OverridepublicbooleanisSatisfiedBy(MetadataPropertymetadataProperty){Matchermatcher=pattern.matcher(metadataProperty.getName());returnmatcher.find();}}}
Exporting Metadata
Export Metadata Properties to CSV
The following code snippets show how to export metadata properties to a CSV file.
try(Metadatametadata=newMetadata(Constants.InputEml)){// We use a predicate that extracts all metadata propertiesIReadOnlyList<MetadataProperty>properties=metadata.findProperties(newAnySpecification());Stringdelimiter=";";StringBuilderbuilder=newStringBuilder();builder.append(String.format("Name%sValue",delimiter));builder.append("\n");for(MetadataPropertyproperty:properties){builder.append(String.format("\"%s\"%s\"%s\"",property.getName(),delimiter,formatValue(property.getValue())));builder.append("\n");}try(PrintWriterout=newPrintWriter(Constants.OutputCsv)){out.println(builder.toString());}}
Note
The implementation of the FormatValue method used in the code sample above can be different depending on your task so we just omitted it here. Please see the full code of the method in the sample project on GitHub.
Using the Replace API
Update Values with IReplaceHandler
The code samples below show how to update metadata properties using a custom filter.
Old API (Implementation of IReplaceHandler)
publicclassAuthorReplaceHandlerimplementsIReplaceHandler<MetadataProperty>{publicAuthorReplaceHandler(StringoutputPath){this.outputPath=outputPath;}publicStringoutputPath;@OverridepublicStringgetOutputPath(){returnoutputPath;}publicbooleanhandle(MetadataPropertyproperty){// if property name is 'author'if("author".equalsIgnoreCase(property.getName())){// update property valueproperty.setValue(newPropertyValue("Jack London"));// and mark property as updatedreturntrue;}// ignore all other propertiesreturnfalse;}}
In the new API there is no need to use the IReplaceHandler interface since you have full control over property filters using the GrooupDocs.Metadata search engine.