
org.nuiton.i18n.plugin.bundle.I18nArtifact Maven / Gradle / Ivy
/*
* #%L
* I18n :: Maven Plugin
* %%
* Copyright (C) 2007 - 2017 Code Lutin, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package org.nuiton.i18n.plugin.bundle;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.maven.artifact.Artifact;
import org.nuiton.i18n.bundle.I18nBundle;
import org.nuiton.i18n.bundle.I18nBundleEntry;
import org.nuiton.i18n.bundle.I18nBundleUtil;
/**
* @author Tony Chemit - [email protected]
* @since 0.12
*/
public class I18nArtifact {
private static final Logger log = LogManager.getLogger(I18nArtifact.class);
private final Artifact artifact;
private final URL url;
protected I18nBundle[] bundles;
I18nArtifact(Artifact artifact) throws MalformedURLException {
this.artifact = artifact;
url = artifact.getFile().toURI().toURL();
}
I18nArtifact(Artifact artifact,
File file) throws MalformedURLException {
this.artifact = artifact;
url = file.toURI().toURL();
}
Artifact getArtifact() {
return artifact;
}
I18nBundleEntry[] getBundleEntries(Locale l, Locale defaultLocale) {
if (bundles == null) {
throw new NullPointerException("le bundleManager n'a pas ete initialise!");
}
return I18nBundleUtil.getBundleEntries(l, defaultLocale, bundles);
}
I18nBundleEntry[] getBundleEntries(Locale l) {
if (bundles == null) {
throw new NullPointerException("le bundleManager n'a pas ete initialise!");
}
return I18nBundleUtil.getBundleEntries(l, bundles);
}
/**
* Detects the i18n bundles for this artifacts, says in i18n directory.
*
* @return {@code true} if artifact has some i18n bunbles, {@code false}
* otherwise.
* @throws IOException if any IO error.
*/
boolean detectBundles() throws IOException {
URL[] i18nUrls = I18nBundleUtil.getURLs(url);
if (i18nUrls == null || i18nUrls.length == 0) {
// aucune url sur un fichier de traduction trouve
// l'artifact n'est pas i18n.
if (log.isDebugEnabled()) {
log.debug(String.format("no i18n url for artifact %s", artifact));
}
return false;
}
List listBundles =
I18nBundleUtil.detectBundles(i18nUrls);
if (listBundles.isEmpty()) {
// pas de bundle instancie (cela ne devrait jamais arrive...)
return false;
}
bundles = listBundles.toArray(new I18nBundle[listBundles.size()]);
return true;
}
@Override
public String toString() {
return String.format("%s:%s:%s", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy