Fix inconsistent indents

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1735061 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2016-03-15 11:54:16 +00:00
parent 4d7955d6b5
commit 5f2f0adfae
1 changed files with 235 additions and 235 deletions

View File

@ -65,14 +65,14 @@ public final class ZipPackage extends Package {
* Constructor. Creates a new, empty ZipPackage. * Constructor. Creates a new, empty ZipPackage.
*/ */
public ZipPackage() { public ZipPackage() {
super(defaultPackageAccess); super(defaultPackageAccess);
this.zipArchive = null; this.zipArchive = null;
try { try {
this.contentTypeManager = new ZipContentTypeManager(null, this); this.contentTypeManager = new ZipContentTypeManager(null, this);
} catch (InvalidFormatException e) { } catch (InvalidFormatException e) {
logger.log(POILogger.WARN,"Could not parse ZipPackage", e); logger.log(POILogger.WARN,"Could not parse ZipPackage", e);
} }
} }
/** /**
@ -88,10 +88,10 @@ public final class ZipPackage extends Package {
* ZipInputStream. * ZipInputStream.
*/ */
ZipPackage(InputStream in, PackageAccess access) throws IOException { ZipPackage(InputStream in, PackageAccess access) throws IOException {
super(access); super(access);
InputStream zis = new ZipInputStream(in); InputStream zis = new ZipInputStream(in);
ThresholdInputStream tis = ZipSecureFile.addThreshold(zis); ThresholdInputStream tis = ZipSecureFile.addThreshold(zis);
this.zipArchive = new ZipInputStreamZipEntrySource(tis); this.zipArchive = new ZipInputStreamZipEntrySource(tis);
} }
/** /**
@ -103,18 +103,18 @@ public final class ZipPackage extends Package {
* The package access mode. * The package access mode.
*/ */
ZipPackage(String path, PackageAccess access) { ZipPackage(String path, PackageAccess access) {
super(access); super(access);
final ZipFile zipFile; final ZipFile zipFile;
try { try {
zipFile = ZipHelper.openZipFile(path); zipFile = ZipHelper.openZipFile(path);
} catch (IOException e) { } catch (IOException e) {
throw new InvalidOperationException( throw new InvalidOperationException(
"Can't open the specified file: '" + path + "'", e); "Can't open the specified file: '" + path + "'", e);
} }
this.zipArchive = new ZipFileZipEntrySource(zipFile); this.zipArchive = new ZipFileZipEntrySource(zipFile);
} }
/** /**
@ -126,18 +126,18 @@ public final class ZipPackage extends Package {
* The package access mode. * The package access mode.
*/ */
ZipPackage(File file, PackageAccess access) { ZipPackage(File file, PackageAccess access) {
super(access); super(access);
final ZipFile zipFile; final ZipFile zipFile;
try { try {
zipFile = ZipHelper.openZipFile(file); zipFile = ZipHelper.openZipFile(file);
} catch (IOException e) { } catch (IOException e) {
throw new InvalidOperationException( throw new InvalidOperationException(
"Can't open the specified file: '" + file + "'", e); "Can't open the specified file: '" + file + "'", e);
} }
this.zipArchive = new ZipFileZipEntrySource(zipFile); this.zipArchive = new ZipFileZipEntrySource(zipFile);
} }
/** /**
@ -151,207 +151,207 @@ public final class ZipPackage extends Package {
* The package access mode. * The package access mode.
*/ */
ZipPackage(ZipEntrySource zipEntry, PackageAccess access) { ZipPackage(ZipEntrySource zipEntry, PackageAccess access) {
super(access); super(access);
this.zipArchive = zipEntry; this.zipArchive = zipEntry;
} }
/** /**
* Retrieves the parts from this package. We assume that the package has not * Retrieves the parts from this package. We assume that the package has not
* been yet inspect to retrieve all the parts, this method will open the * been yet inspect to retrieve all the parts, this method will open the
* archive and look for all parts contain inside it. If the package part * archive and look for all parts contain inside it. If the package part
* list is not empty, it will be emptied. * list is not empty, it will be emptied.
* *
* @return All parts contain in this package. * @return All parts contain in this package.
* @throws InvalidFormatException * @throws InvalidFormatException
* Throws if the package is not valid. * Throws if the package is not valid.
*/ */
@Override @Override
protected PackagePart[] getPartsImpl() throws InvalidFormatException { protected PackagePart[] getPartsImpl() throws InvalidFormatException {
if (this.partList == null) { if (this.partList == null) {
// The package has just been created, we create an empty part // The package has just been created, we create an empty part
// list. // list.
this.partList = new PackagePartCollection(); this.partList = new PackagePartCollection();
} }
if (this.zipArchive == null) { if (this.zipArchive == null) {
return this.partList.values().toArray( return this.partList.values().toArray(
new PackagePart[this.partList.values().size()]); new PackagePart[this.partList.values().size()]);
} }
// First we need to parse the content type part
Enumeration<? extends ZipEntry> entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().equalsIgnoreCase(
ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
try {
this.contentTypeManager = new ZipContentTypeManager(
getZipArchive().getInputStream(entry), this);
} catch (IOException e) {
throw new InvalidFormatException(e.getMessage());
}
break;
}
}
// At this point, we should have loaded the content type part // First we need to parse the content type part
if (this.contentTypeManager == null) { Enumeration<? extends ZipEntry> entries = this.zipArchive.getEntries();
// Is it a different Zip-based format? while (entries.hasMoreElements()) {
boolean hasMimetype = false; ZipEntry entry = entries.nextElement();
boolean hasSettingsXML = false; if (entry.getName().equalsIgnoreCase(
entries = this.zipArchive.getEntries(); ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
while (entries.hasMoreElements()) { try {
ZipEntry entry = entries.nextElement(); this.contentTypeManager = new ZipContentTypeManager(
if (entry.getName().equals("mimetype")) { getZipArchive().getInputStream(entry), this);
hasMimetype = true; } catch (IOException e) {
} throw new InvalidFormatException(e.getMessage());
}
break;
}
}
// At this point, we should have loaded the content type part
if (this.contentTypeManager == null) {
// Is it a different Zip-based format?
boolean hasMimetype = false;
boolean hasSettingsXML = false;
entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().equals("mimetype")) {
hasMimetype = true;
}
if (entry.getName().equals("settings.xml")) { if (entry.getName().equals("settings.xml")) {
hasSettingsXML = true; hasSettingsXML = true;
} }
} }
if (hasMimetype && hasSettingsXML) { if (hasMimetype && hasSettingsXML) {
throw new ODFNotOfficeXmlFileException( throw new ODFNotOfficeXmlFileException(
"The supplied data appears to be in ODF (Open Document) Format. " + "The supplied data appears to be in ODF (Open Document) Format. " +
"Formats like these (eg ODS, ODP) are not supported, try Apache ODFToolkit"); "Formats like these (eg ODS, ODP) are not supported, try Apache ODFToolkit");
} }
// Fallback exception
throw new InvalidFormatException(
"Package should contain a content type part [M1.13]");
}
// Now create all the relationships // Fallback exception
// (Need to create relationships before other throw new InvalidFormatException(
// parts, otherwise we might create a part before "Package should contain a content type part [M1.13]");
// its relationship exists, and then it won't tie up) }
entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
PackagePartName partName = buildPartName(entry);
if(partName == null) continue;
// Only proceed for Relationships at this stage // Now create all the relationships
String contentType = contentTypeManager.getContentType(partName); // (Need to create relationships before other
if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) { // parts, otherwise we might create a part before
try { // its relationship exists, and then it won't tie up)
partList.put(partName, new ZipPackagePart(this, entry, entries = this.zipArchive.getEntries();
partName, contentType)); while (entries.hasMoreElements()) {
} catch (InvalidOperationException e) { ZipEntry entry = entries.nextElement();
throw new InvalidFormatException(e.getMessage()); PackagePartName partName = buildPartName(entry);
} if(partName == null) continue;
}
}
// Then we can go through all the other parts // Only proceed for Relationships at this stage
entries = this.zipArchive.getEntries(); String contentType = contentTypeManager.getContentType(partName);
while (entries.hasMoreElements()) { if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
ZipEntry entry = entries.nextElement(); try {
PackagePartName partName = buildPartName(entry); partList.put(partName, new ZipPackagePart(this, entry,
if(partName == null) continue; partName, contentType));
} catch (InvalidOperationException e) {
throw new InvalidFormatException(e.getMessage());
}
}
}
String contentType = contentTypeManager // Then we can go through all the other parts
.getContentType(partName); entries = this.zipArchive.getEntries();
if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) { while (entries.hasMoreElements()) {
// Already handled ZipEntry entry = entries.nextElement();
} PackagePartName partName = buildPartName(entry);
else if (contentType != null) { if(partName == null) continue;
try {
partList.put(partName, new ZipPackagePart(this, entry,
partName, contentType));
} catch (InvalidOperationException e) {
throw new InvalidFormatException(e.getMessage());
}
} else {
throw new InvalidFormatException(
"The part "
+ partName.getURI().getPath()
+ " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]");
}
}
return partList.values().toArray(new ZipPackagePart[partList.size()]); String contentType = contentTypeManager
} .getContentType(partName);
if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
// Already handled
}
else if (contentType != null) {
try {
partList.put(partName, new ZipPackagePart(this, entry,
partName, contentType));
} catch (InvalidOperationException e) {
throw new InvalidFormatException(e.getMessage());
}
} else {
throw new InvalidFormatException(
"The part "
+ partName.getURI().getPath()
+ " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]");
}
}
/** return partList.values().toArray(new ZipPackagePart[partList.size()]);
* Builds a PackagePartName for the given ZipEntry, }
* or null if it's the content types / invalid part
*/
private PackagePartName buildPartName(ZipEntry entry) {
try {
// We get an error when we parse [Content_Types].xml
// because it's not a valid URI.
if (entry.getName().equalsIgnoreCase(
ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
return null;
}
return PackagingURIHelper.createPartName(ZipHelper
.getOPCNameFromZipItemName(entry.getName()));
} catch (Exception e) {
// We assume we can continue, even in degraded mode ...
logger.log(POILogger.WARN,"Entry "
+ entry.getName()
+ " is not valid, so this part won't be add to the package.", e);
return null;
}
}
/** /**
* Create a new MemoryPackagePart from the specified URI and content type * Builds a PackagePartName for the given ZipEntry,
* * or null if it's the content types / invalid part
* */
* aram partName The part URI. private PackagePartName buildPartName(ZipEntry entry) {
* try {
* @param contentType // We get an error when we parse [Content_Types].xml
* The part content type. // because it's not a valid URI.
* @return The newly created zip package part, else <b>null</b>. if (entry.getName().equalsIgnoreCase(
*/ ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
@Override return null;
protected PackagePart createPartImpl(PackagePartName partName, }
String contentType, boolean loadRelationships) { return PackagingURIHelper.createPartName(ZipHelper
if (contentType == null) .getOPCNameFromZipItemName(entry.getName()));
throw new IllegalArgumentException("contentType"); } catch (Exception e) {
// We assume we can continue, even in degraded mode ...
logger.log(POILogger.WARN,"Entry "
+ entry.getName()
+ " is not valid, so this part won't be add to the package.", e);
return null;
}
}
if (partName == null) /**
throw new IllegalArgumentException("partName"); * Create a new MemoryPackagePart from the specified URI and content type
*
*
* aram partName The part URI.
*
* @param contentType
* The part content type.
* @return The newly created zip package part, else <b>null</b>.
*/
@Override
protected PackagePart createPartImpl(PackagePartName partName,
String contentType, boolean loadRelationships) {
if (contentType == null)
throw new IllegalArgumentException("contentType");
try { if (partName == null)
return new MemoryPackagePart(this, partName, contentType, throw new IllegalArgumentException("partName");
loadRelationships);
} catch (InvalidFormatException e) {
logger.log(POILogger.WARN, e);
return null;
}
}
/** try {
* Delete a part from the package return new MemoryPackagePart(this, partName, contentType,
* loadRelationships);
* @throws IllegalArgumentException } catch (InvalidFormatException e) {
* Throws if the part URI is nulll or invalid. logger.log(POILogger.WARN, e);
*/ return null;
@Override }
protected void removePartImpl(PackagePartName partName) { }
if (partName == null)
throw new IllegalArgumentException("partUri");
}
/** /**
* Flush the package. Do nothing. * Delete a part from the package
*/ *
@Override * @throws IllegalArgumentException
protected void flushImpl() { * Throws if the part URI is nulll or invalid.
// Do nothing */
} @Override
protected void removePartImpl(PackagePartName partName) {
if (partName == null)
throw new IllegalArgumentException("partUri");
}
/** /**
* Close and save the package. * Flush the package. Do nothing.
* */
* @see #close() @Override
*/ protected void flushImpl() {
@Override // Do nothing
protected void closeImpl() throws IOException { }
// Flush the package
flush(); /**
* Close and save the package.
*
* @see #close()
*/
@Override
protected void closeImpl() throws IOException {
// Flush the package
flush();
// Save the content // Save the content
if (this.originalPackagePath != null if (this.originalPackagePath != null
@ -418,20 +418,20 @@ public final class ZipPackage extends Package {
} }
} }
/** /**
* Implement the getPart() method to retrieve a part from its URI in the * Implement the getPart() method to retrieve a part from its URI in the
* current package * current package
* *
* *
* @see #getPart(PackageRelationship) * @see #getPart(PackageRelationship)
*/ */
@Override @Override
protected PackagePart getPartImpl(PackagePartName partName) { protected PackagePart getPartImpl(PackagePartName partName) {
if (partList.containsKey(partName)) { if (partList.containsKey(partName)) {
return partList.get(partName); return partList.get(partName);
} }
return null; return null;
} }
/** /**
* Save this package into the specified stream * Save this package into the specified stream
@ -525,14 +525,14 @@ public final class ZipPackage extends Package {
"Fail to save: an error occurs while saving the package : " "Fail to save: an error occurs while saving the package : "
+ e.getMessage(), e); + e.getMessage(), e);
} }
} }
/** /**
* Get the zip archive * Get the zip archive
* *
* @return The zip archive. * @return The zip archive.
*/ */
public ZipEntrySource getZipArchive() { public ZipEntrySource getZipArchive() {
return zipArchive; return zipArchive;
} }
} }