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();}}}
Comparing Metadata
Get the Difference between Two Sets of Metadata Properties
The code samples below demonstrate how to get the difference of two collections of properties extracted from different files.
// The specification to extract metadata properties we want to compare
// In this code sample we retrieve built-in document properties
ContainsTagSpecificationspecification=newContainsTagSpecification(Tags.getDocument().getBuiltIn());try(Metadatametadata1=newMetadata(Constants.InputDoc);Metadatametadata2=newMetadata(Constants.InputDocx)){IReadOnlyList<MetadataProperty>properties1=metadata1.findProperties(specification);IReadOnlyList<MetadataProperty>properties2=metadata2.findProperties(specification);// You can implement your own equality comparer for metadata properties
// In this code sample, we just use the one that worked in previous versions of GroupDocs.Metadata
MetadataPropertyEqualityComparercomparer=newMetadataPropertyEqualityComparer();for(MetadataPropertyproperty:properties1){booleanfound=false;for(MetadataPropertyother:properties2){if(comparer.equals(property,other)){found=true;break;}}if(!found){System.out.println(String.format("%s = %s",property.getName(),property.getValue()));}}}
Get the Intersection of Two Sets of Metadata Properties
The code samples below demonstrate how to get the intersection of two collections of properties extracted from different files.
// The specification to extract metadata properties we want to compare
// In this code sample we retrieve TIFF/EXIF tags
Specificationspecification=newSpecification(){@OverridepublicbooleanisSatisfiedBy(MetadataPropertyproperty){returnpropertyinstanceofTiffTag;}};try(Metadatametadata1=newMetadata(Constants.JpegWithExif);Metadatametadata2=newMetadata(Constants.TiffWithExif)){IReadOnlyList<MetadataProperty>properties1=metadata1.findProperties(specification);IReadOnlyList<MetadataProperty>properties2=metadata2.findProperties(specification);// You can implement your own equality comparer for metadata properties
// In this code sample, we just use the one that worked in previous versions of GroupDocs.Metadata
MetadataPropertyEqualityComparercomparer=newMetadataPropertyEqualityComparer();for(MetadataPropertyproperty:properties1){for(MetadataPropertyother:properties2){if(comparer.equals(property,other)){System.out.println(String.format("%s = %s",property.getName(),property.getValue()));break;}}}}
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.