|
| 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 | +} |
0 commit comments