/**
   * Resolves a reference to a local element identified by address and identifier, where {@code
   * linkBase} identifies a document, including the current document, and {@code linkRef} is the id
   * of the desired element.
   *
   * <p>If {@code linkBase} refers to a local COLLADA file and {@code linkRef} is non-null, the
   * return value is the element identified by {@code linkRef}. If {@code linkRef} is null, the
   * return value is a parsed {@link ColladaRoot} for the COLLADA file identified by {@code
   * linkBase}. Otherwise, {@code linkBase} is returned.
   *
   * @param linkBase the address of the document containing the requested element.
   * @param linkRef the element's identifier.
   * @return the requested element, or null if the element is not found.
   * @throws IllegalArgumentException if the address is null.
   */
  protected Object resolveLocalReference(String linkBase, String linkRef) {
    if (linkBase == null) {
      String message = Logging.getMessage("nullValue.DocumentSourceIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    try {
      File file = new File(linkBase);

      if (!file.exists()) return null;

      // Determine whether the file is a COLLADA document. If not, just return the file path.
      if (!WWIO.isContentType(file, ColladaConstants.COLLADA_MIME_TYPE))
        return file.toURI().toString();

      // Attempt to open and parse the COLLADA file.
      ColladaRoot refRoot = ColladaRoot.createAndParse(file);
      // An exception is thrown if parsing fails, so no need to check for null.

      // Add the parsed file to the session cache so it doesn't have to be parsed again.
      WorldWind.getSessionCache().put(linkBase, refRoot);

      // Now check the newly opened COLLADA file for the referenced item, if a reference was
      // specified.
      if (linkRef != null) return refRoot.getItemByID(linkRef);
      else return refRoot;
    } catch (Exception e) {
      String message =
          Logging.getMessage("generic.UnableToResolveReference", linkBase + "/" + linkRef);
      Logging.logger().warning(message);
      return null;
    }
  }
 /** Closes the event stream associated with this context's XML event reader. */
 protected void closeEventStream() {
   try {
     this.eventStream.close();
     this.eventStream = null;
   } catch (IOException e) {
     String message = Logging.getMessage("generic.ExceptionClosingXmlEventReader");
     Logging.logger().warning(message);
   }
 }
  /**
   * Specifies this shape's geographic position. The position's altitude is relative to this shape's
   * altitude mode.
   *
   * @param position this shape's geographic position.
   * @throws IllegalArgumentException if the position is null.
   */
  public void setPosition(Position position) {
    if (position == null) {
      String message = Logging.getMessage("nullValue.PositionIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    this.position = position;
  }
  /**
   * Create a new <code>ColladaRoot</code> for a {@link ColladaDoc} instance. A ColladaDoc
   * represents COLLADA files from either files or input streams.
   *
   * @param docSource the ColladaDoc instance representing the COLLADA document.
   * @throws IllegalArgumentException if the document source is null.
   * @throws IOException if an error occurs while reading the COLLADA document.
   */
  public ColladaRoot(ColladaDoc docSource) throws IOException {
    super(ColladaConstants.COLLADA_NAMESPACE);

    if (docSource == null) {
      String message = Logging.getMessage("nullValue.DocumentSourceIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    this.colladaDoc = docSource;
    this.initialize();
  }
  /**
   * Create a new <code>ColladaRoot</code> for a {@link URL}.
   *
   * @param docSource the URL of the document.
   * @throws IllegalArgumentException if the document source is null.
   * @throws IOException if an error occurs while reading the Collada document.
   */
  public ColladaRoot(URL docSource) throws IOException {
    super(ColladaConstants.COLLADA_NAMESPACE);

    if (docSource == null) {
      String message = Logging.getMessage("nullValue.DocumentSourceIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    URLConnection conn = docSource.openConnection();
    this.colladaDoc = new ColladaInputStream(conn.getInputStream(), WWIO.makeURI(docSource));

    this.initialize();
  }
  /**
   * Resolves a reference to a local or remote file or element. If the link refers to an element in
   * the current document, this method returns that element. If the link refers to a remote
   * document, this method will initiate asynchronous retrieval of the document, and return a URL of
   * the downloaded document in the file cache, if it is available locally. If the link identifies a
   * COLLADA document, the document will be returned as a parsed ColladaRoot.
   *
   * @param link the address of the document or element to resolve. This may be a full URL, a URL
   *     fragment that identifies an element in the current document ("#myElement"), or a URL and a
   *     fragment identifier ("http://server.com/model.dae#myElement").
   * @return the requested element, or null if the element is not found.
   * @throws IllegalArgumentException if the address is null.
   */
  public Object resolveReference(String link) {
    if (link == null) {
      String message = Logging.getMessage("nullValue.DocumentSourceIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    try {
      String[] linkParts = link.split("#");
      String linkBase = linkParts[0];
      String linkRef = linkParts.length > 1 ? linkParts[1] : null;

      // See if it's a reference to an internal element.
      if (WWUtil.isEmpty(linkBase) && !WWUtil.isEmpty(linkRef)) return this.getItemByID(linkRef);

      // Interpret the path relative to the current document.
      String path = this.getSupportFilePath(linkBase);
      if (path == null) path = linkBase;

      // See if it's an already found and parsed COLLADA file.
      Object o = WorldWind.getSessionCache().get(path);
      if (o != null && o instanceof ColladaRoot)
        return linkRef != null ? ((ColladaRoot) o).getItemByID(linkRef) : o;

      URL url = WWIO.makeURL(path);
      if (url == null) {
        // See if the reference can be resolved to a local file.
        o = this.resolveLocalReference(path, linkRef);
      }

      // If we didn't find a local file, treat it as a remote reference.
      if (o == null) o = this.resolveRemoteReference(path, linkRef);

      if (o != null) return o;

      // If the reference was not resolved as a remote reference, look for a local element
      // identified by the
      // reference string. This handles the case of malformed internal references that omit the #
      // sign at the
      // beginning of the reference.
      return this.getItemByID(link);
    } catch (Exception e) {
      String message = Logging.getMessage("generic.UnableToResolveReference", link);
      Logging.logger().warning(message);
    }

    return null;
  }
  /**
   * Resolves a reference to a remote element identified by address and identifier, where {@code
   * linkBase} identifies a remote document, and {@code linkRef} is the id of the desired element.
   * This method retrieves resources asynchronously using the {@link
   * gov.nasa.worldwind.cache.FileStore}.
   *
   * <p>The return value is null if the file is not yet available in the FileStore. If {@code
   * linkBase} refers to a COLLADA file and {@code linkRef} is non-null, the return value is the
   * element identified by {@code linkRef}. If {@code linkBase} refers to a COLLADA file and {@code
   * linkRef} is null, the return value is a parsed {@link ColladaRoot} for the COLLADA file
   * identified by {@code linkBase}. Otherwise the return value is a {@link URL} to the file in the
   * file cache.
   *
   * @param linkBase the address of the document containing the requested element.
   * @param linkRef the element's identifier.
   * @return URL to the requested file, parsed ColladaRoot, or COLLADA element. Returns null if the
   *     document is not yet available in the FileStore.
   * @throws IllegalArgumentException if the {@code linkBase} is null.
   */
  public Object resolveRemoteReference(String linkBase, String linkRef) {
    if (linkBase == null) {
      String message = Logging.getMessage("nullValue.DocumentSourceIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    try {
      // See if it's in the cache. If not, requestFile will start another thread to retrieve it and
      // return null.
      URL url = WorldWind.getDataFileStore().requestFile(linkBase);
      if (url == null) return null;

      // It's in the cache. If it's a COLLADA file try to parse it so we can search for the
      // specified reference.
      // If it's not COLLADA, just return the url for the cached file.
      String contentType = WorldWind.getDataFileStore().getContentType(linkBase);
      if (contentType == null) {
        String suffix = WWIO.getSuffix(linkBase.split(";")[0]); // strip of trailing garbage
        if (!WWUtil.isEmpty(suffix)) contentType = WWIO.makeMimeTypeForSuffix(suffix);
      }

      if (!this.canParseContentType(contentType)) return url;

      // If the file is a COLLADA document, attempt to open it. We can't open it as a File with
      // createAndParse
      // because the ColladaRoot that will be created needs to have the remote address in order to
      // resolve any
      // relative references within it.
      ColladaRoot refRoot = this.parseCachedColladaFile(url, linkBase);

      // Add the parsed file to the session cache so it doesn't have to be parsed again.
      WorldWind.getSessionCache().put(linkBase, refRoot);

      // Now check the newly opened COLLADA file for the referenced item, if a reference was
      // specified.
      if (linkRef != null) return refRoot.getItemByID(linkRef);
      else return refRoot;
    } catch (Exception e) {
      String message =
          Logging.getMessage("generic.UnableToResolveReference", linkBase + "/" + linkRef);
      Logging.logger().warning(message);
      return null;
    }
  }
  /**
   * Called just before the constructor returns. If overriding this method be sure to invoke <code>
   * super.initialize()</code>.
   *
   * @throws java.io.IOException if an I/O error occurs attempting to open the document source.
   */
  protected void initialize() throws IOException {
    this.eventStream = new BufferedInputStream(this.getColladaDoc().getInputStream());
    this.eventReader = this.createReader(this.eventStream);
    if (this.eventReader == null)
      throw new WWRuntimeException(
          Logging.getMessage("XML.UnableToOpenDocument", this.getColladaDoc()));

    this.parserContext = this.createParserContext(this.eventReader);
  }
  public Double[] getLayerExtremeElevations(WMSCapabilities caps, String[] layerNames) {
    if (caps == null) {
      String message = Logging.getMessage("nullValue.WMSCapabilities");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    if (layerNames == null) {
      String message = Logging.getMessage("nullValue.WMSLayerNames");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    Double extremeMin = null;
    Double extremeMax = null;

    for (String name : layerNames) {
      WMSLayerCapabilities layer = caps.getLayerByName(name);
      if (layer == null) continue;

      Double min = layer.getExtremeElevationMin();
      if (min != null && (extremeMin == null || min.compareTo(min) > 0)) extremeMin = min;

      Double max = layer.getExtremeElevationMax();
      if (max != null && (extremeMax == null || max.compareTo(max) > 0)) extremeMax = max;
    }

    if (extremeMin != null || extremeMax != null) {
      Double[] extremes = new Double[] {null, null};

      if (extremeMin != null) extremes[0] = extremeMin;
      if (extremeMax != null) extremes[1] = extremeMax;

      return extremes;
    }

    return null;
  }
  /**
   * Creates a Collada root for an untyped source. The source must be either a {@link File} or a
   * {@link String} identifying either a file path or a {@link URL}. Null is returned if the source
   * type is not recognized.
   *
   * @param docSource either a {@link File} or a {@link String} identifying a file path or {@link
   *     URL}.
   * @return a new {@link ColladaRoot} for the specified source, or null if the source type is not
   *     supported.
   * @throws IllegalArgumentException if the source is null.
   * @throws IOException if an error occurs while reading the source.
   */
  public static ColladaRoot create(Object docSource) throws IOException {
    if (docSource == null) {
      String message = Logging.getMessage("nullValue.DocumentSourceIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    if (docSource instanceof File) {
      return new ColladaRoot((File) docSource);
    } else if (docSource instanceof URL) {
      return new ColladaRoot((URL) docSource);
    } else if (docSource instanceof String) {
      File file = new File((String) docSource);
      if (file.exists()) return new ColladaRoot(file);

      URL url = WWIO.makeURL(docSource);
      if (url != null) return new ColladaRoot(url);
    } else if (docSource instanceof InputStream) {
      return new ColladaRoot((InputStream) docSource);
    }

    return null;
  }
  /**
   * Creates and parses a Collada root for an untyped source. The source must be either a {@link
   * File} or a {@link String} identifying either a file path or a {@link URL}. Null is returned if
   * the source type is not recognized.
   *
   * @param docSource either a {@link File} or a {@link String} identifying a file path or {@link
   *     URL}.
   * @return a new {@link ColladaRoot} for the specified source, or null if the source type is not
   *     supported.
   * @throws IllegalArgumentException if the source is null.
   * @throws IOException if an error occurs while reading the source.
   */
  public static ColladaRoot createAndParse(Object docSource)
      throws IOException, XMLStreamException {
    ColladaRoot colladaRoot = ColladaRoot.create(docSource);

    if (colladaRoot == null) {
      String message =
          Logging.getMessage(
              "generic.UnrecognizedSourceTypeOrUnavailableSource", docSource.toString());
      throw new IllegalArgumentException(message);
    }

    colladaRoot.parse();

    return colladaRoot;
  }
  public Long getLayerLatestLastUpdateTime(WMSCapabilities caps, String[] layerNames) {
    if (caps == null) {
      String message = Logging.getMessage("nullValue.WMSCapabilities");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    if (layerNames == null) {
      String message = Logging.getMessage("nullValue.WMSLayerNames");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    String lastUpdate = null;

    for (String name : layerNames) {
      WMSLayerCapabilities layer = this.getLayerByName(name);
      if (layer == null) continue;

      String update = layer.getLastUpdate();
      if (update != null
          && update.length() > 0
          && (lastUpdate == null || update.compareTo(lastUpdate) > 0)) lastUpdate = update;
    }

    if (lastUpdate != null) {
      try {
        return Long.parseLong(lastUpdate);
      } catch (NumberFormatException e) {
        String message = Logging.getMessage("generic.ConversionError", lastUpdate);
        Logging.logger().warning(message);
      }
    }

    return null;
  }