use of com.helger.xml.util.mime.MimeTypeInfo.ExtensionWithSource in project ph-commons by phax.
the class MainReadSharedMimeInfo method main.
public static void main(final String[] args) throws MimeTypeParserException {
LOGGER.info("Reading shared-mime-info/freedesktop.org.xml");
final IMicroDocument aDoc = MicroReader.readMicroXML(new File("src/test/resources/shared-mime-info/freedesktop.org.xml.in"));
if (aDoc == null)
throw new IllegalStateException("Failed to read mime type info file!");
final MimeTypeInfoManager aMgr = new MimeTypeInfoManager();
for (final IMicroElement eSrcMimeType : aDoc.getDocumentElement().getAllChildElements(NS, "mime-type")) {
final String sMIMEType = eSrcMimeType.getAttributeValue("type");
final ICommonsOrderedSet<MimeTypeWithSource> aLocalNames = new CommonsLinkedHashSet<>();
// Names
aLocalNames.add(new MimeTypeWithSource(sMIMEType));
for (final IMicroElement eSrcChild : eSrcMimeType.getAllChildElements(NS, "alias")) {
final String sAlias = eSrcChild.getAttributeValue("type");
aLocalNames.add(new MimeTypeWithSource(sAlias));
}
// Description
String sComment = null;
for (final IMicroElement eSrcChild : eSrcMimeType.getAllChildElements(NS, "comment")) if (!eSrcChild.hasAttribute("xml:lang")) {
sComment = eSrcChild.getTextContentTrimmed();
break;
}
// Sub class of
final ICommonsOrderedSet<String> aSubClassOf = new CommonsLinkedHashSet<>();
for (final IMicroElement eSrcChild : eSrcMimeType.getAllChildElements(NS, "sub-class-of")) {
final String s = eSrcChild.getAttributeValue("type");
aSubClassOf.add(s);
}
boolean bHasAnyGlob = false;
final ICommonsOrderedSet<String> aGlobs = new CommonsLinkedHashSet<>();
final ICommonsOrderedSet<ExtensionWithSource> aExts = new CommonsLinkedHashSet<>();
for (final IMicroElement eSrcChild : eSrcMimeType.getAllChildElements(NS, "glob")) {
final String sPattern = eSrcChild.getAttributeValue("pattern");
if (RegExHelper.stringMatchesPattern("\\*\\.[0-9a-zA-Z]+", sPattern)) {
final String sExt = sPattern.substring(2);
aExts.add(new ExtensionWithSource(sExt));
} else
aGlobs.add(sPattern);
bHasAnyGlob = true;
}
if (bHasAnyGlob) {
// Append only if at least on filename pattern is present
aMgr.registerMimeType(new MimeTypeInfo(aLocalNames, sComment, aSubClassOf, aGlobs, aExts, "shared-mime-info"));
}
}
LOGGER.info("Read " + aMgr.getAllMimeTypeInfos().size() + " mime type infos");
// Maps file extension to MIME type
LOGGER.info("Reading shared-mime-info/fileext-mimetype-mapping-local.xml");
final Map<String, String> FileExtMap = new HashMap<>();
if (XMLMapHandler.readMap(new FileSystemResource("src/test/resources/shared-mime-info/fileext-mimetype-mapping-local.xml"), FileExtMap).isFailure())
throw new InitializationException("Failed to init file extension to mimetype mapping file");
LOGGER.info("Read " + FileExtMap.size() + " entries");
// Check old data
for (final Map.Entry<String, String> aEntry : CollectionHelper.getSortedByKey(FileExtMap).entrySet()) {
final String sOldExt = aEntry.getKey();
final String sOldMimeType = aEntry.getValue();
final MimeType aOldMimeType = MimeTypeParser.parseMimeType(sOldMimeType);
ICommonsList<MimeTypeInfo> aNew;
// First check for Mime Type, as they are unique
aNew = aMgr.getAllInfosOfMimeType(aOldMimeType);
if (aNew != null) {
// Mime type is present - check if extension is also present
boolean bFound = false;
for (final MimeTypeInfo aInfo : aNew) if (aInfo.containsExtension(sOldExt)) {
bFound = true;
break;
}
if (!bFound) {
if (aNew.size() == 1) {
aMgr.addExtension(aNew.get(0), new ExtensionWithSource(sOldExt, "old"));
if (false)
LOGGER.info("Added extension '" + sOldExt + "' to " + sOldMimeType + "!");
} else
LOGGER.error(sOldMimeType + ": '" + sOldExt + "' not found in " + aNew + "!");
}
} else {
// no such mime type present - Check other direction: ext 2 mimetype
aNew = aMgr.getAllInfosOfExtension(sOldExt);
if (aNew != null) {
// Found extension - check if MIME type matches that type
boolean bFound = false;
for (final MimeTypeInfo aInfo : aNew) if (aInfo.containsMimeType(sOldMimeType)) {
bFound = true;
break;
}
if (!bFound) {
if (aNew.size() == 1) {
aMgr.addMimeType(aNew.get(0), new MimeTypeWithSource(aOldMimeType, "old"));
if (false)
LOGGER.info("'" + sOldExt + "': " + sOldMimeType + " not found in " + aNew.get(0) + "!");
} else
LOGGER.error("'" + sOldExt + "': " + sOldMimeType + " not found in any of " + aNew + "!");
}
} else {
// No such mapping from ext to mime type
// Create a new entry
aMgr.registerMimeType(new MimeTypeInfo(CollectionHelper.newOrderedSet(new MimeTypeWithSource(sOldMimeType)), null, new CommonsLinkedHashSet<>(), new CommonsLinkedHashSet<>(), CollectionHelper.newOrderedSet(new ExtensionWithSource(sOldExt)), "old"));
if (false)
LOGGER.info("Creating new: " + sOldMimeType + " = '" + sOldExt + "'");
}
}
}
LOGGER.info("Finally having " + aMgr.getAllMimeTypeInfos().size() + " mime type infos");
if (MicroWriter.writeToFile(aMgr.getAsDocument(), new File("src/main/resources/codelists/mime-type-info.xml")).isSuccess())
LOGGER.info("done - run mvn license:format !!");
else
LOGGER.error("Error writing file");
}
use of com.helger.xml.util.mime.MimeTypeInfo.ExtensionWithSource in project ph-commons by phax.
the class MimeTypeInfoManager method registerMimeType.
public void registerMimeType(@Nonnull final MimeTypeInfo aInfo) {
ValueEnforcer.notNull(aInfo, "Info");
final ICommonsSet<MimeTypeWithSource> aMimeTypes = aInfo.getAllMimeTypesWithSource();
final ICommonsSet<ExtensionWithSource> aExtensions = aInfo.getAllExtensionsWithSource();
// Check if MimeType is unique
// Note: Extension must not be unique
m_aRWLock.readLocked(() -> {
for (final MimeTypeWithSource aMimeType : aMimeTypes) {
final ICommonsList<MimeTypeInfo> aExisting = m_aMapMimeType.get(aMimeType.getMimeType());
if (aExisting != null)
throw new IllegalArgumentException("Cannot register " + aInfo + ". A mapping for mime type '" + aMimeType + "' is already registered: " + aExisting);
}
});
// Perform changes
m_aRWLock.writeLocked(() -> {
m_aList.add(aInfo);
for (final MimeTypeWithSource aMimeType : aMimeTypes) m_aMapMimeType.computeIfAbsent(aMimeType.getMimeType(), k -> new CommonsArrayList<>()).add(aInfo);
for (final ExtensionWithSource aExt : aExtensions) m_aMapExt.computeIfAbsent(aExt.getExtension(), k -> new CommonsArrayList<>()).add(aInfo);
});
}
use of com.helger.xml.util.mime.MimeTypeInfo.ExtensionWithSource in project ph-commons by phax.
the class MimeTypeInfoMicroTypeConverter method convertToNative.
@Nullable
public MimeTypeInfo convertToNative(@Nonnull final IMicroElement aElement) {
final ICommonsOrderedSet<MimeTypeWithSource> aMimeTypes = new CommonsLinkedHashSet<>();
for (final IMicroElement eMimeType : aElement.getAllChildElements(ELEMENT_MIMETYPE)) {
try {
final MimeType aMimeType = MimeTypeParser.parseMimeType(eMimeType.getTextContentTrimmed());
final String sSource = eMimeType.getAttributeValue(ATTR_SOURCE);
aMimeTypes.add(new MimeTypeWithSource(aMimeType, sSource));
} catch (final MimeTypeParserException ex) {
throw new IllegalStateException("Failed to parse MIME type", ex);
}
}
final String sComment = MicroHelper.getChildTextContent(aElement, ELEMENT_COMMENT);
final ICommonsOrderedSet<String> aParentTypes = new CommonsLinkedHashSet<>();
for (final IMicroElement eParentType : aElement.getAllChildElements(ELEMENT_PARENT_TYPE)) aParentTypes.add(eParentType.getTextContentTrimmed());
final ICommonsOrderedSet<String> aGlobs = new CommonsLinkedHashSet<>();
for (final IMicroElement eGlob : aElement.getAllChildElements(ELEMENT_GLOB)) aGlobs.add(eGlob.getTextContentTrimmed());
final ICommonsOrderedSet<ExtensionWithSource> aExtensions = new CommonsLinkedHashSet<>();
for (final IMicroElement eExtension : aElement.getAllChildElements(ELEMENT_EXTENSION)) {
// May be null if the empty extension ("") is used
final String sExtension = StringHelper.getNotNull(eExtension.getTextContentTrimmed());
final String sSource = eExtension.getAttributeValue(ATTR_SOURCE);
aExtensions.add(new ExtensionWithSource(sExtension, sSource));
}
final String sSource = aElement.getAttributeValue(ATTR_SOURCE);
return new MimeTypeInfo(aMimeTypes, sComment, aParentTypes, aGlobs, aExtensions, sSource);
}
use of com.helger.xml.util.mime.MimeTypeInfo.ExtensionWithSource in project ph-commons by phax.
the class MimeTypeInfoMicroTypeConverter method convertToMicroElement.
@Nullable
public IMicroElement convertToMicroElement(@Nonnull final MimeTypeInfo aObject, @Nullable final String sNamespaceURI, @Nonnull final String sTagName) {
final IMicroElement eRet = new MicroElement(sNamespaceURI, sTagName);
for (final MimeTypeWithSource aMimeType : aObject.getAllMimeTypesWithSource()) {
eRet.appendElement(sNamespaceURI, ELEMENT_MIMETYPE).setAttribute(ATTR_SOURCE, aMimeType.getSource()).appendText(aMimeType.getMimeTypeAsString());
}
if (aObject.hasComment())
eRet.appendElement(sNamespaceURI, ELEMENT_COMMENT).appendText(aObject.getComment());
for (final String sParentType : aObject.getAllParentTypes()) eRet.appendElement(sNamespaceURI, ELEMENT_PARENT_TYPE).appendText(sParentType);
for (final String sGlob : aObject.getAllGlobs()) eRet.appendElement(sNamespaceURI, ELEMENT_GLOB).appendText(sGlob);
for (final ExtensionWithSource aExtension : aObject.getAllExtensionsWithSource()) {
eRet.appendElement(sNamespaceURI, ELEMENT_EXTENSION).setAttribute(ATTR_SOURCE, aExtension.getSource()).appendText(aExtension.getExtension());
}
eRet.setAttribute(ATTR_SOURCE, aObject.getSource());
return eRet;
}
Aggregations