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

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 // First we need to parse the content type part
Enumeration<? extends ZipEntry> entries = this.zipArchive.getEntries(); Enumeration<? extends ZipEntry> entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement(); ZipEntry entry = entries.nextElement();
if (entry.getName().equalsIgnoreCase( if (entry.getName().equalsIgnoreCase(
ContentTypeManager.CONTENT_TYPES_PART_NAME)) { ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
try { try {
this.contentTypeManager = new ZipContentTypeManager( this.contentTypeManager = new ZipContentTypeManager(
getZipArchive().getInputStream(entry), this); getZipArchive().getInputStream(entry), this);
} catch (IOException e) { } catch (IOException e) {
throw new InvalidFormatException(e.getMessage()); throw new InvalidFormatException(e.getMessage());
} }
break; break;
} }
} }
// At this point, we should have loaded the content type part // At this point, we should have loaded the content type part
if (this.contentTypeManager == null) { if (this.contentTypeManager == null) {
// Is it a different Zip-based format? // Is it a different Zip-based format?
boolean hasMimetype = false; boolean hasMimetype = false;
boolean hasSettingsXML = false; boolean hasSettingsXML = false;
entries = this.zipArchive.getEntries(); entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement(); ZipEntry entry = entries.nextElement();
if (entry.getName().equals("mimetype")) { if (entry.getName().equals("mimetype")) {
hasMimetype = true; 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 // Fallback exception
throw new InvalidFormatException( throw new InvalidFormatException(
"Package should contain a content type part [M1.13]"); "Package should contain a content type part [M1.13]");
} }
// Now create all the relationships // Now create all the relationships
// (Need to create relationships before other // (Need to create relationships before other
// parts, otherwise we might create a part before // parts, otherwise we might create a part before
// its relationship exists, and then it won't tie up) // its relationship exists, and then it won't tie up)
entries = this.zipArchive.getEntries(); entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement(); ZipEntry entry = entries.nextElement();
PackagePartName partName = buildPartName(entry); PackagePartName partName = buildPartName(entry);
if(partName == null) continue; if(partName == null) continue;
// Only proceed for Relationships at this stage // Only proceed for Relationships at this stage
String contentType = contentTypeManager.getContentType(partName); String contentType = contentTypeManager.getContentType(partName);
if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) { if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
try { try {
partList.put(partName, new ZipPackagePart(this, entry, partList.put(partName, new ZipPackagePart(this, entry,
partName, contentType)); partName, contentType));
} catch (InvalidOperationException e) { } catch (InvalidOperationException e) {
throw new InvalidFormatException(e.getMessage()); throw new InvalidFormatException(e.getMessage());
} }
} }
} }
// Then we can go through all the other parts // Then we can go through all the other parts
entries = this.zipArchive.getEntries(); entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement(); ZipEntry entry = entries.nextElement();
PackagePartName partName = buildPartName(entry); PackagePartName partName = buildPartName(entry);
if(partName == null) continue; if(partName == null) continue;
String contentType = contentTypeManager String contentType = contentTypeManager
.getContentType(partName); .getContentType(partName);
if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) { if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
// Already handled // Already handled
} }
else if (contentType != null) { else if (contentType != null) {
try { try {
partList.put(partName, new ZipPackagePart(this, entry, partList.put(partName, new ZipPackagePart(this, entry,
partName, contentType)); partName, contentType));
} catch (InvalidOperationException e) { } catch (InvalidOperationException e) {
throw new InvalidFormatException(e.getMessage()); throw new InvalidFormatException(e.getMessage());
} }
} else { } else {
throw new InvalidFormatException( throw new InvalidFormatException(
"The part " "The part "
+ partName.getURI().getPath() + partName.getURI().getPath()
+ " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]"); + " 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()]); return partList.values().toArray(new ZipPackagePart[partList.size()]);
} }
/** /**
* Builds a PackagePartName for the given ZipEntry, * Builds a PackagePartName for the given ZipEntry,
* or null if it's the content types / invalid part * or null if it's the content types / invalid part
*/ */
private PackagePartName buildPartName(ZipEntry entry) { private PackagePartName buildPartName(ZipEntry entry) {
try { try {
// We get an error when we parse [Content_Types].xml // We get an error when we parse [Content_Types].xml
// because it's not a valid URI. // because it's not a valid URI.
if (entry.getName().equalsIgnoreCase( if (entry.getName().equalsIgnoreCase(
ContentTypeManager.CONTENT_TYPES_PART_NAME)) { ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
return null; return null;
} }
return PackagingURIHelper.createPartName(ZipHelper return PackagingURIHelper.createPartName(ZipHelper
.getOPCNameFromZipItemName(entry.getName())); .getOPCNameFromZipItemName(entry.getName()));
} catch (Exception e) { } catch (Exception e) {
// We assume we can continue, even in degraded mode ... // We assume we can continue, even in degraded mode ...
logger.log(POILogger.WARN,"Entry " logger.log(POILogger.WARN,"Entry "
+ entry.getName() + entry.getName()
+ " is not valid, so this part won't be add to the package.", e); + " is not valid, so this part won't be add to the package.", e);
return null; return null;
} }
} }
/** /**
* Create a new MemoryPackagePart from the specified URI and content type * Create a new MemoryPackagePart from the specified URI and content type
* *
* *
* aram partName The part URI. * aram partName The part URI.
* *
* @param contentType * @param contentType
* The part content type. * The part content type.
* @return The newly created zip package part, else <b>null</b>. * @return The newly created zip package part, else <b>null</b>.
*/ */
@Override @Override
protected PackagePart createPartImpl(PackagePartName partName, protected PackagePart createPartImpl(PackagePartName partName,
String contentType, boolean loadRelationships) { String contentType, boolean loadRelationships) {
if (contentType == null) if (contentType == null)
throw new IllegalArgumentException("contentType"); throw new IllegalArgumentException("contentType");
if (partName == null) if (partName == null)
throw new IllegalArgumentException("partName"); throw new IllegalArgumentException("partName");
try { try {
return new MemoryPackagePart(this, partName, contentType, return new MemoryPackagePart(this, partName, contentType,
loadRelationships); loadRelationships);
} catch (InvalidFormatException e) { } catch (InvalidFormatException e) {
logger.log(POILogger.WARN, e); logger.log(POILogger.WARN, e);
return null; return null;
} }
} }
/** /**
* Delete a part from the package * Delete a part from the package
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
* Throws if the part URI is nulll or invalid. * Throws if the part URI is nulll or invalid.
*/ */
@Override @Override
protected void removePartImpl(PackagePartName partName) { protected void removePartImpl(PackagePartName partName) {
if (partName == null) if (partName == null)
throw new IllegalArgumentException("partUri"); throw new IllegalArgumentException("partUri");
} }
/** /**
* Flush the package. Do nothing. * Flush the package. Do nothing.
*/ */
@Override @Override
protected void flushImpl() { protected void flushImpl() {
// Do nothing // Do nothing
} }
/** /**
* Close and save the package. * Close and save the package.
* *
* @see #close() * @see #close()
*/ */
@Override @Override
protected void closeImpl() throws IOException { protected void closeImpl() throws IOException {
// Flush the package // Flush the package
flush(); 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;
} }
} }