/* * 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.sis.storage.geotiff;import java.util.Locale;import java.util.Iterator;import java.util.Map;import java.util.StringJoiner;import java.util.logging.Filter;import java.util.logging.LogRecord;import java.io.IOException;import java.io.StringReader;import java.io.ByteArrayInputStream;import java.nio.ByteBuffer;import java.nio.charset.StandardCharsets;import java.time.Instant;import javax.xml.stream.XMLEventReader;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamException;import javax.xml.stream.events.XMLEvent;import javax.xml.stream.events.Attribute;import javax.xml.stream.events.Characters;import javax.xml.stream.events.StartElement;import javax.xml.transform.stax.StAXSource;import javax.xml.bind.JAXBException;import javax.xml.namespace.QName;import org.apache.sis.internal.util.StandardDateFormat;import org.apache.sis.internal.storage.MetadataBuilder;import org.apache.sis.storage.event.StoreListeners;import org.apache.sis.util.collection.TreeTable;import org.apache.sis.util.collection.DefaultTreeTable;import org.apache.sis.util.collection.TableColumn;import org.apache.sis.util.resources.Errors;import org.apache.sis.xml.XML;import static org.apache.sis.internal.util.TemporalUtilities.toDate;/** * Supports for metadata encoded in XML inside a GeoTIFF tags. * This is a temporary object used only at parsing time. * Two TIFF tags are associated to XML data: * *
*
{@code GDAL_METADATA} (A480) stored as ASCII characters.
*
{@code GEO_METADATA} (C6DD) stored as bytes with UTF-8 encoding.
*
* * {@code GEO_METADATA} is defined by the Defense Geospatial Information Working Group (DGIWG) * in the GeoTIFF Profile for Georeferenced Imagery standard. * * @author Martin Desruisseaux (Geomatys) * @version 1.2 * * @see DGIWG Standards * * @since 1.2 */final class XMLMetadata implements Filter { /** * The {@value} string, used in GDAL metadata. */ private static final String ITEM = "Item", NAME = "name"; /** * The bytes to decode as an XML document. * DGIWG specification mandates UTF-8 encoding. */ private byte[] bytes; /** * The XML document as string. */ private String string; /** * Name of the XML element being processed. Used for error message only: * this field is left to a non-null value if an exception occurred during XML parsing. */ private String currentElement; /** * Where to report non-fatal warnings. */ private final StoreListeners listeners; /** * {@code true} if the XML is GDAL metadata. Example: * * {@snippet lang="xml" : * * 2016-09-08T15:53:00+05:00 * 2016-09-08T15:56:00+05:00 * * } */ private final boolean isGDAL; /** * The next metadata in a list of linked metadata. Should always be {@code null}, * but we nevertheless define this field in case a file defines more than one * {@code GEO_METADATA} or {@code GDAL_METADATA} tags. */ private XMLMetadata next; /** * Creates a new instance with the given XML. Used for testing purposes. */ XMLMetadata(final String xml, final boolean isGDAL) { this.isGDAL = isGDAL; this.string = xml; listeners = null; } /** * Creates new metadata which will decode the given vector of bytes. * * @param reader the TIFF reader. * @param type type of the metadata tag to read. * @param count number of bytes or characters in the value to read. * @param tag the tag where the metadata was stored. */ XMLMetadata(final Reader reader, final Type type, final long count, final short tag) throws IOException { isGDAL = (tag == Tags.GDAL_METADATA); listeners = reader.store.listeners(); switch (type) { case ASCII: { final String[] cs = type.readString(reader.input, count, reader.store.encoding); switch (cs.length) { case 0: break; case 1: string = cs[0]; break; // Usual case. default: string = String.join(System.lineSeparator(), cs); break; } break; } case BYTE: case UBYTE: { /* * NoSuchElementException, ClassCastException and UnsupportedOperationException * should never happen here because we verified that the vector type is byte. */ bytes = ((ByteBuffer) type.readVector(reader.input, count).buffer().get()).array(); break; } } } /** * Appends this metadata at the end of a linked list starting with the given element. * This method is inefficient because it iterates over all elements for reaching the tail, * but it should not be an issue because this method is invoked only in the unlikely case * where a file would define more than one {@code *_METADATA} tag. * * @param head first element of the linked list where to append this metadata. */ final void appendTo(XMLMetadata head) { while (head.next != null) { head = head.next; } head.next = this; } /** * Returns the name of the tag from which the XML has been read. * This is used for error messages. */ String tag() { return Tags.name(isGDAL ? Tags.GDAL_METADATA : Tags.GEO_METADATA); } /** * Returns {@code true} if the XML document could not be read. */ public boolean isEmpty() { return bytes == null && string == null; } /** * Returns the XML document as a character string, or {@code null} if the document could not be read. */ public String toString() { if (string == null) { if (bytes == null) { return null; } string = new String(bytes, StandardCharsets.UTF_8); } return string; } /** * Returns a reader for the XML document, or {@code null} if the document could not be read. */ private XMLEventReader toXML() throws XMLStreamException { // BUG: CWE-611: Improper Restriction of XML External Entity Reference// final XMLInputFactory factory = XMLInputFactory.newFactory();// FIXED: if (bytes != null) { return factory.createXMLEventReader(new ByteArrayInputStream(bytes), "UTF-8"); } else if (string != null) { return factory.createXMLEventReader(new StringReader(string)); } else { return null; } } /** * A tree-table representation of the XML document contained in the enclosing {@link XMLMetadata}. * The root node contains the XML document as a {@linkplain #getUserObject() user object}. * It allows JavaFX application to support the "copy to clipboard" operation. */ static final class Root extends DefaultTreeTable.Node { /** * Column for the name associated to the element. */ private static final TableColumn NAME = NativeMetadata.NAME; /** * Column for the value associated to the element. */ private static final TableColumn