下面列出了java.util.jar.Attributes#Name ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void writeEntry(OutputStream os, Attributes.Name name,
String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException {
String nameString = name.toString();
os.write(nameString.getBytes(StandardCharsets.US_ASCII));
os.write(VALUE_SEPARATOR);
encoder.reset();
bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2);
CharBuffer cBuf = CharBuffer.wrap(value);
while (true) {
CoderResult r = encoder.encode(cBuf, bBuf, true);
if (CoderResult.UNDERFLOW == r) {
r = encoder.flush(bBuf);
}
os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
os.write(LINE_SEPARATOR);
if (CoderResult.UNDERFLOW == r) {
break;
}
os.write(' ');
bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
}
}
private byte[] manifestContent(@Nullable String targetLabel, @Nullable String injectingRuleKind)
throws IOException {
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
Attributes.Name createdBy = new Attributes.Name("Created-By");
if (attributes.getValue(createdBy) == null) {
attributes.put(createdBy, "bazel");
}
if (targetLabel != null) {
// Enable add_deps support. add_deps expects this attribute in the jar manifest.
attributes.putValue("Target-Label", targetLabel);
}
if (injectingRuleKind != null) {
// add_deps support for aspects. Usually null.
attributes.putValue("Injecting-Rule-Kind", injectingRuleKind);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
manifest.write(out);
return out.toByteArray();
}
private static byte[] manifestContent(TurbineOptions turbineOptions) throws IOException {
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
Attributes.Name createdBy = new Attributes.Name("Created-By");
if (attributes.getValue(createdBy) == null) {
attributes.put(createdBy, "bazel");
}
if (turbineOptions.targetLabel().isPresent()) {
attributes.put(TARGET_LABEL, turbineOptions.targetLabel().get());
}
if (turbineOptions.injectingRuleKind().isPresent()) {
attributes.put(INJECTING_RULE_KIND, turbineOptions.injectingRuleKind().get());
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
manifest.write(out);
return out.toByteArray();
}
@Override
public Set<String> findJarsWithMatchingTags(Set<String> classpath, Set<String> tagsToFind) {
Set<String> tagSourceJars = new LinkedHashSet<>();
for (String jar : classpath) {
if (!Files.exists(Paths.get(jar))) {
LOGGER.severe("Classpath entry not found: " + jar);
continue;
}
try (JarFile jarFile = new JarFile(jar)) {
Manifest manifest = jarFile.getManifest();
Attributes.Name name = new Attributes.Name(SMITHY_TAG_PROPERTY);
if (manifest == null || !manifest.getMainAttributes().containsKey(name)) {
continue;
}
Set<String> jarTags = loadTags((String) manifest.getMainAttributes().get(name));
LOGGER.info("Found Smithy-Tags in JAR dependency `" + jar + "`: " + jarTags);
for (String needle : tagsToFind) {
if (jarTags.contains(needle)) {
tagSourceJars.add(jar);
break;
}
}
} catch (IOException e) {
throw new SmithyBuildException(
"Error reading manifest from JAR in build dependencies: " + e.getMessage(), e);
}
}
return tagSourceJars;
}
private static void fixManifest(Manifest manifest, TinyRemapper remapper) {
Attributes mainAttrs = manifest.getMainAttributes();
if (remapper != null) {
String val = mainAttrs.getValue(Attributes.Name.MAIN_CLASS);
if (val != null) mainAttrs.put(Attributes.Name.MAIN_CLASS, mapFullyQualifiedClassName(val, remapper));
val = mainAttrs.getValue("Launcher-Agent-Class");
if (val != null) mainAttrs.put("Launcher-Agent-Class", mapFullyQualifiedClassName(val, remapper));
}
mainAttrs.remove(Attributes.Name.SIGNATURE_VERSION);
for (Iterator<Attributes> it = manifest.getEntries().values().iterator(); it.hasNext(); ) {
Attributes attrs = it.next();
for (Iterator<Object> it2 = attrs.keySet().iterator(); it2.hasNext(); ) {
Attributes.Name attrName = (Attributes.Name) it2.next();
String name = attrName.toString();
if (name.endsWith("-Digest") || name.contains("-Digest-") || name.equals("Magic")) {
it2.remove();
}
}
if (attrs.isEmpty()) it.remove();
}
}
public static void main(String[] args) throws Exception {
Attributes.Name k0 = Name.MANIFEST_VERSION;
Attributes.Name k1 = Name.MAIN_CLASS;
Attributes.Name k2 = Name.SEALED;
String v0 = "42.0";
String v1 = "com.google.Hello";
String v2 = "yes";
checkOrder(k0, v0, k1, v1, k2, v2);
checkOrder(k1, v1, k0, v0, k2, v2);
checkOrder(k2, v2, k1, v1, k0, v0);
}
public static void main(String[] args) throws Exception {
try {
Attributes.Name name = new Attributes.Name("");
throw new Exception("empty string should be rejected");
} catch (IllegalArgumentException e) {
}
}
private void writeAttributes(Attributes attributes, OutputStream out) throws IOException {
List<Attributes.Name> sortedNames =
attributes.keySet().stream()
.map(a -> (Attributes.Name) a)
.sorted(Comparator.comparing(Attributes.Name::toString))
.collect(Collectors.toList());
for (Attributes.Name name : sortedNames) {
writeKeyValue(name.toString(), attributes.getValue(name), out);
}
writeLine(out);
}
public static Manifest createManifest() {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_URL, "http://samoa.yahoo.com");
manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VERSION, "0.1");
manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VENDOR, "Yahoo");
manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VENDOR_ID, "SAMOA");
Attributes s4Attributes = new Attributes();
s4Attributes.putValue("S4-App-Class", "path.to.Class");
Attributes.Name name = new Attributes.Name("S4-App-Class");
Attributes.Name S4Version = new Attributes.Name("S4-Version");
manifest.getMainAttributes().put(name, "samoa.topology.impl.DoTaskApp");
manifest.getMainAttributes().put(S4Version, "0.6.0-incubating");
return manifest;
}
public static void main(String[] args) throws Exception {
try {
Attributes.Name name = new Attributes.Name("");
throw new Exception("empty string should be rejected");
} catch (IllegalArgumentException e) {
}
}
/**
* java.util.jar.Attributes#equals(java.lang.Object)
*/
public void test_equalsLjava_lang_Object() {
Attributes.Name n1 = new Attributes.Name("name"), n2 = new Attributes.Name("Name");
assertEquals(n1, n2);
Attributes a1 = new Attributes();
a1.putValue("one", "1");
a1.putValue("two", "2");
Attributes a2 = new Attributes();
a2.putValue("One", "1");
a2.putValue("TWO", "2");
assertEquals(a1, a2);
assertEquals(a1, a1);
a2 = null;
assertFalse(a1.equals(a2));
}
public static void main(String[] args) throws Exception {
try {
Attributes.Name name = new Attributes.Name("");
throw new Exception("empty string should be rejected");
} catch (IllegalArgumentException e) {
}
}
/**
* Compares two manifest files. First check is for number of attributes,
* next one is comparing name-value pairs.
*
*@param mf1, mf2 manifests to compare
*@param ignoredEntries array of manifest entries to ignore
*@return true if files contains the same entries/values in manifest
*/
public static boolean equalsManifest(File mf1, File mf2, String[] ignoredEntries) {
if (ignoredEntries == null) {
ignoredEntries = new String[] {};
}
try {
Manifest m1 = new Manifest(new FileInputStream(mf1));
Manifest m2 = new Manifest(new FileInputStream(mf2));
Attributes a1 = m1.getMainAttributes();
Attributes a2 = m2.getMainAttributes();
if (a1.size() != a2.size()) {
return false;
}
for (Iterator<Object> i = a1.keySet().iterator(); i.hasNext();) {
Attributes.Name a = (Attributes.Name) i.next();
boolean b = true;
for (int j = 0; j < ignoredEntries.length; j++) {
if (a.toString().equals(ignoredEntries[j])) {
a2.remove(a);
b = false;
break;
}
}
if (b && (a1.get(a).equals(a2.get(a)))) {
a2.remove(a);
}
}
return a2.isEmpty();
} catch (FileNotFoundException fnfe) {
LOGGER.log(Level.WARNING, "Exception from test - comparing manifests", fnfe); //NOI18N
} catch (IOException ioe) {
LOGGER.log(Level.WARNING, "Exception from test - comparing manifests", ioe); //NOI18N
}
return false;
}
static void writeAttribute(OutputStream out, Attributes.Name name, String value)
throws IOException {
writeAttribute(out, name.toString(), value);
}
/**
* Returns attribute value from a manifest main section,
* or null if missing or a file does not contain a manifest.
*/
@Nullable
public static String getJarAttribute(@Nonnull File file, @Nonnull Attributes.Name attribute) {
return getJarAttributeImpl(file, null, attribute);
}
public static Attributes getAttribute(String name, String value) {
Attributes a = new Attributes();
Attributes.Name attribName = new Attributes.Name(name);
a.put(attribName, value);
return a;
}
@LogMessage(level = WARN)
@Message(id = 45, value = "Extension %s is missing the required manifest attribute %s-%s (skipping extension)")
void extensionMissingManifestAttribute(String item, String again, Attributes.Name suffix);
private static String getAttr(Attributes spec, Attributes main, Attributes.Name name) {
String val = null;
if (spec != null) val = spec.getValue (name);
if (val == null && main != null) val = main.getValue (name);
return val;
}
/**
* Returns an attribute of the package containing the resource.
*/
public String getPackageAttribute(final Attributes.Name attrName) throws FileSystemException {
return (String) packageFolder.getContent().getAttribute(attrName.toString());
}
/**
* Find a localized and/or branded value in a JAR manifest.
* @param attr the manifest attributes
* @param key the key to look for (case-insensitive)
* @param locale the locale to use
* @return the value if found, else <code>null</code>
*/
public static String getLocalizedValue(Attributes attr, Attributes.Name key, Locale locale) {
return getLocalizedValue(attr2Map(attr), key.toString().toLowerCase(Locale.US), locale);
}