Skip to content

Commit acff62c

Browse files
committed
Prepared for releasec
1 parent fcc17d9 commit acff62c

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ Add the following to your pom.xml to use this artifact, replacing `x.y.z` with t
3636

3737
# News and Noteworthy
3838

39-
* v0.1.0 - 2024-01-30
39+
* v0.1.0 - 2024-01-31
4040
* Initial version
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2024 Philip Helger (www.helger.com)
3+
* philip[at]helger[dot]com
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.helger.phive.binary.impl;
18+
19+
import java.util.Map;
20+
21+
import javax.annotation.Nonnull;
22+
23+
import com.helger.commons.collection.ArrayHelper;
24+
import com.helger.commons.collection.impl.CommonsArrayList;
25+
import com.helger.commons.collection.impl.CommonsHashMap;
26+
import com.helger.commons.mime.CMimeType;
27+
import com.helger.phive.binary.AbstractFileFormatDescriptor;
28+
import com.helger.phive.binary.EPhiveContentValidationMode;
29+
import com.helger.phive.binary.IPhiveContentValidator;
30+
31+
/**
32+
* File format descriptor for JPG/JPEG
33+
*
34+
* @author Philip Helger
35+
*/
36+
public class FileFormatDescriptorJPG extends AbstractFileFormatDescriptor
37+
{
38+
private static final byte [] MIME_ID_JPG = { (byte) 0xff, (byte) 0xd8 };
39+
40+
@Nonnull
41+
private static Map <EPhiveContentValidationMode, IPhiveContentValidator> _getContentValidators ()
42+
{
43+
final Map <EPhiveContentValidationMode, IPhiveContentValidator> ret = new CommonsHashMap <> ();
44+
ret.put (EPhiveContentValidationMode.LEADING_BYTES, data -> ArrayHelper.startsWith (data, MIME_ID_JPG));
45+
return ret;
46+
}
47+
48+
public FileFormatDescriptorJPG ()
49+
{
50+
super ("JPEG image",
51+
"JPEG",
52+
new CommonsArrayList <> ("jpg", "jpeg"),
53+
new CommonsArrayList <> (CMimeType.IMAGE_JPG.getAsString ()),
54+
_getContentValidators ());
55+
}
56+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (C) 2024 Philip Helger (www.helger.com)
3+
* philip[at]helger[dot]com
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.helger.phive.binary.impl;
18+
19+
import java.util.Map;
20+
21+
import javax.annotation.Nonnull;
22+
23+
import com.helger.commons.charset.EUnicodeBOM;
24+
import com.helger.commons.collection.ArrayHelper;
25+
import com.helger.commons.collection.impl.CommonsArrayList;
26+
import com.helger.commons.collection.impl.CommonsHashMap;
27+
import com.helger.commons.collection.impl.ICommonsList;
28+
import com.helger.commons.mime.CMimeType;
29+
import com.helger.phive.binary.AbstractFileFormatDescriptor;
30+
import com.helger.phive.binary.EPhiveContentValidationMode;
31+
import com.helger.phive.binary.IPhiveContentValidator;
32+
33+
/**
34+
* File format descriptor for XML documents
35+
*
36+
* @author Philip Helger
37+
*/
38+
public class FileFormatDescriptorXML extends AbstractFileFormatDescriptor
39+
{
40+
private static final ICommonsList <byte []> PREFIXES = new CommonsArrayList <> (8 * EUnicodeBOM.values ().length);
41+
static
42+
{
43+
// Add all XML mime types: as the combination of all BOMs and all character
44+
// encodings as determined by
45+
// http://www.w3.org/TR/REC-xml/#sec-guessing
46+
final ICommonsList <byte []> aXMLStuff = new CommonsArrayList <> ();
47+
// UCS4
48+
aXMLStuff.add (new byte [] { 0x3c, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00 });
49+
aXMLStuff.add (new byte [] { 0x00, 0x3c, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00 });
50+
aXMLStuff.add (new byte [] { 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x3f, 0x00 });
51+
aXMLStuff.add (new byte [] { 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x3f });
52+
// UTF-16
53+
aXMLStuff.add (new byte [] { 0x00, 0x3c, 0x00, 0x3f });
54+
aXMLStuff.add (new byte [] { 0x3c, 0x00, 0x3f, 0x00 });
55+
// ISO-8859-1/UTF-8/ASCII etc.
56+
aXMLStuff.add (new byte [] { 0x3c, 0x3f, 0x78, 0x6d });
57+
// EBCDIC
58+
aXMLStuff.add (new byte [] { 0x4c, 0x6f, (byte) 0xa7, (byte) 0x94 });
59+
60+
// Remember all types without the BOM
61+
PREFIXES.addAll (aXMLStuff);
62+
63+
// Remember all types with each BOM
64+
for (final EUnicodeBOM eBOM : EUnicodeBOM.values ())
65+
for (final byte [] aXML : aXMLStuff)
66+
{
67+
final byte [] aData = ArrayHelper.getConcatenated (eBOM.getAllBytes (), aXML);
68+
PREFIXES.add (aData);
69+
}
70+
}
71+
72+
@Nonnull
73+
private static Map <EPhiveContentValidationMode, IPhiveContentValidator> _getContentValidators ()
74+
{
75+
final Map <EPhiveContentValidationMode, IPhiveContentValidator> ret = new CommonsHashMap <> ();
76+
ret.put (EPhiveContentValidationMode.LEADING_BYTES, data -> {
77+
for (final byte [] aPrefix : PREFIXES)
78+
if (ArrayHelper.startsWith (data, aPrefix))
79+
return true;
80+
return false;
81+
});
82+
return ret;
83+
}
84+
85+
public FileFormatDescriptorXML ()
86+
{
87+
super ("XML",
88+
"XML",
89+
new CommonsArrayList <> ("xml"),
90+
new CommonsArrayList <> (CMimeType.APPLICATION_XML.getAsString (), CMimeType.TEXT_XML.getAsString ()),
91+
_getContentValidators ());
92+
}
93+
}

src/main/java/com/helger/phive/binary/impl/FileFormatRegistrarDefaultSPI.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ public void registerFileFormats (@Nonnull final IFileFormatRegistry aRegistry)
3434
{
3535
aRegistry.registerFileFormat (new FileFormatDescriptorCSV ());
3636
aRegistry.registerFileFormat (new FileFormatDescriptorGIF ());
37+
aRegistry.registerFileFormat (new FileFormatDescriptorJPG ());
3738
aRegistry.registerFileFormat (new FileFormatDescriptorPDF ());
3839
aRegistry.registerFileFormat (new FileFormatDescriptorPNG ());
3940
aRegistry.registerFileFormat (new FileFormatDescriptorPSD ());
4041
aRegistry.registerFileFormat (new FileFormatDescriptorTIFF ());
4142
aRegistry.registerFileFormat (new FileFormatDescriptorXLS ());
4243
aRegistry.registerFileFormat (new FileFormatDescriptorXLSX ());
44+
aRegistry.registerFileFormat (new FileFormatDescriptorXML ());
4345
}
4446
}

0 commit comments

Comments
 (0)