-
Notifications
You must be signed in to change notification settings - Fork 197
IMAGING-168 installing package with Swedish characters adds junk char… #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.ByteOrder; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.IllegalCharsetNameException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
|
@@ -42,6 +44,8 @@ | |
|
||
public class IptcParser extends BinaryFileParser { | ||
private static final ByteOrder APP13_BYTE_ORDER = ByteOrder.BIG_ENDIAN; | ||
private static final String DEFAULT_ENCODING = "ISO-8859-1"; | ||
private static final String UTF_8 = "utf8"; | ||
|
||
public IptcParser() { | ||
setByteOrder(ByteOrder.BIG_ENDIAN); | ||
|
@@ -125,6 +129,7 @@ public PhotoshopApp13Data parsePhotoshopSegment(final byte[] bytes, | |
protected List<IptcRecord> parseIPTCBlock(final byte[] bytes, final boolean verbose) | ||
throws IOException { | ||
final List<IptcRecord> elements = new ArrayList<IptcRecord>(); | ||
String encoding = DEFAULT_ENCODING; | ||
|
||
int index = 0; | ||
// Integer recordVersion = null; | ||
|
@@ -190,6 +195,11 @@ protected List<IptcRecord> parseIPTCBlock(final byte[] bytes, final boolean verb | |
// Debug.debug("recordSize", recordSize + " (0x" | ||
// + Integer.toHexString(recordSize) + ")"); | ||
|
||
if(recordNumber == IptcConstants.IPTC_ENVELOPE_RECORD_NUMBER && recordType == IptcConstants.IPTC_ENV_TAG_CODED_CHARACTER_SET){ | ||
encoding = getEncodingCharsetName(recordData); | ||
continue; | ||
} | ||
|
||
if (recordNumber != IptcConstants.IPTC_APPLICATION_2_RECORD_NUMBER) { | ||
continue; | ||
} | ||
|
@@ -226,7 +236,7 @@ protected List<IptcRecord> parseIPTCBlock(final byte[] bytes, final boolean verb | |
// continue; | ||
// } | ||
|
||
final String value = new String(recordData, "ISO-8859-1"); | ||
final String value = new String(recordData, encoding); | ||
|
||
final IptcType iptcType = IptcTypeLookup.getIptcType(recordType); | ||
|
||
|
@@ -248,6 +258,35 @@ protected List<IptcRecord> parseIPTCBlock(final byte[] bytes, final boolean verb | |
return elements; | ||
} | ||
|
||
private String getEncodingCharsetName(byte[] codedCharacterSet){ | ||
final Character WHITESPACE = ' '; | ||
String codedCharacterSetString = new String(codedCharacterSet); | ||
try { | ||
if (Charset.isSupported(codedCharacterSetString)) { | ||
return codedCharacterSetString; | ||
} | ||
}catch (IllegalCharsetNameException e){ | ||
|
||
}catch (IllegalArgumentException e){ | ||
|
||
} | ||
//check if encoding is utf8 escape sequence | ||
byte[] utf8EscSeq = new byte[]{'\u001B','%','G'}; | ||
int j=0; | ||
boolean match = true; | ||
for(byte character : codedCharacterSet){ | ||
if(!WHITESPACE.equals(character) && utf8EscSeq[j++] != character) { | ||
match = false; | ||
} | ||
} | ||
|
||
if(match){ | ||
return UTF_8; | ||
} | ||
|
||
return DEFAULT_ENCODING; | ||
} | ||
|
||
protected List<IptcBlock> parseAllBlocks(final byte[] bytes, final boolean verbose, | ||
final boolean strict) throws ImageReadException, IOException { | ||
final List<IptcBlock> blocks = new ArrayList<IptcBlock>(); | ||
|
@@ -438,8 +477,8 @@ public int compare(final IptcRecord e1, final IptcRecord e2) { | |
} | ||
bos.write(element.iptcType.getType()); | ||
|
||
final byte[] recordData = element.value.getBytes("ISO-8859-1"); | ||
if (!new String(recordData, "ISO-8859-1").equals(element.value)) { | ||
final byte[] recordData = element.value.getBytes(DEFAULT_ENCODING); | ||
if (!new String(recordData, DEFAULT_ENCODING).equals(element.value)) { | ||
throw new ImageWriteException( | ||
"Invalid record value, not ISO-8859-1"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also use `DEFAULT_ENCODING There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, this way we won't forget changing the exception text 👍 |
||
} | ||
|
@@ -456,5 +495,4 @@ public int compare(final IptcRecord e1, final IptcRecord e2) { | |
|
||
return blockData; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,18 +38,22 @@ public IptcRecord(final IptcType iptcType, final byte[] bytes, final String valu | |
this.value = value; | ||
} | ||
|
||
public IptcRecord(final IptcType iptcType, final String value) { | ||
public IptcRecord(final IptcType iptcType, final String value, final String charsetName) { | ||
this.iptcType = iptcType; | ||
byte[] tempBytes; | ||
try { | ||
tempBytes = value.getBytes("ISO-8859-1"); | ||
tempBytes = value.getBytes(charsetName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overall I think the code looks good to be merged. I would need to find some spec/docs for reference to clear up a few more questions about the issue. But the main issue right now are the conflicts. The Then we can resolve the conflicts and leave it ready to be merged 👍 |
||
} catch (final UnsupportedEncodingException cannotHappen) { | ||
tempBytes = null; | ||
} | ||
this.bytes = tempBytes; | ||
this.value = value; | ||
} | ||
|
||
public IptcRecord(final IptcType iptcType, final String value) { | ||
this(iptcType, value, "ISO-8859-1"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should better use a constant value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 there is a constant defined in another class, but I wonder if we could use |
||
} | ||
|
||
public byte[] getRawBytes() { | ||
return bytes.clone(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.commons.imaging.formats.jpeg.iptc; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add the Apache License Header There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @britter made changes as per suggestions. Please review now. |
||
|
||
import org.apache.commons.imaging.ImagingTestConstants; | ||
import org.apache.commons.imaging.common.ImageMetadata; | ||
import org.apache.commons.imaging.common.bytesource.ByteSource; | ||
import org.apache.commons.imaging.common.bytesource.ByteSourceFile; | ||
import org.apache.commons.imaging.formats.jpeg.JpegImageParser; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.io.File; | ||
import java.nio.charset.Charset; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import static org.junit.Assert.fail; | ||
|
||
|
||
@RunWith(Parameterized.class) | ||
public class IptcCodedCharacterSetTest extends IptcBaseTest { | ||
|
||
private File imageFile; | ||
|
||
@Parameterized.Parameters | ||
public static Collection<File> data() throws Exception { | ||
return Collections.singleton(new File(ImagingTestConstants.TEST_IMAGE_FOLDER, "iptc/2/test.jpeg")); | ||
} | ||
|
||
public IptcCodedCharacterSetTest(File imageFile) { | ||
this.imageFile = imageFile; | ||
} | ||
|
||
@Test | ||
public void testCodedCharacterSet() throws Exception { | ||
byte[] bytePatternToCompare = new byte[] | ||
{-28,-68,-102,-26,-124,-113,-27,-83,-105}; | ||
|
||
String requiredCaption = new String( bytePatternToCompare , "utf8"); | ||
String metadataName = "Caption/Abstract"; | ||
|
||
final ByteSource byteSource = new ByteSourceFile(imageFile); | ||
JpegImageParser jpegImageParser = new JpegImageParser(); | ||
ImageMetadata metadata = jpegImageParser.getMetadata(byteSource, null); | ||
for (ImageMetadata.ImageMetadataItem item : metadata.getItems()) { | ||
String metadataVal = item.toString(); | ||
String[] metadataKeyValuePair = metadataVal.split(":", 2); | ||
if (metadataKeyValuePair.length > 1 && metadataKeyValuePair[0].equalsIgnoreCase(metadataName) && !metadataKeyValuePair[1].trim().equals(requiredCaption)) { | ||
fail("metadata extraction failed"); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth to add at least one comment saying why these exceptions are not important, and that can be safely ignored.