georssparser.java

来自「world wind java sdk 源码」· Java 代码 · 共 554 行 · 第 1/2 页

JAVA
554
字号
/*
Copyright (C) 2001, 2006 United States Government
as represented by the Administrator of the
National Aeronautics and Space Administration.
All Rights Reserved.
*/
package gov.nasa.worldwind.formats.georss;

import gov.nasa.worldwind.exception.WWRuntimeException;
import gov.nasa.worldwind.geom.*;
import gov.nasa.worldwind.render.*;
import gov.nasa.worldwind.util.Logging;
import org.w3c.dom.*;
import org.xml.sax.*;

import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.*;
import java.io.*;
import java.util.*;
import java.util.logging.Level;

/**
 * @author tag
 * @version $Id: GeoRSSParser.java 3272 2007-10-10 21:00:19Z tgaskins $
 */

public class GeoRSSParser
{
    public static final String GEORSS_URI = "http://www.georss.org/georss";
    public static final String GML_URI = "http://www.opengis.net/gml";

    public static List<Renderable> parseFragment(String fragmentString, NamespaceContext nsc)
    {
        return parseShapes(fixNamespaceQualification(fragmentString));
    }

    public static List<Renderable> parseShapes(String docString)
    {
        if (docString == null)
        {
            String message = Logging.getMessage("nullValue.StringIsNull");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }

        if (docString.length() < 1) // avoid empty strings
            return null;

        try
        {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            docBuilderFactory.setNamespaceAware(true);
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new InputSource(new StringReader(docString)));

            List<Renderable> shapes = parseShapes(doc);

            if (shapes == null || shapes.size() < 1)
            {
                Logging.logger().log(Level.WARNING, "GeoRSS.NoShapes", docString);
                return null;
            }

            return shapes;
        }
        catch (ParserConfigurationException e)
        {
            String message = Logging.getMessage("GeoRSS.ParserConfigurationException");
            Logging.logger().log(Level.SEVERE, message, e);
            throw new WWRuntimeException(message, e);
        }
        catch (IOException e)
        {
            String message = Logging.getMessage("GeoRSS.IOExceptionParsing", docString);
            Logging.logger().log(Level.SEVERE, message, e);
            throw new WWRuntimeException(message, e);
        }
        catch (SAXException e)
        {
            String message = Logging.getMessage("GeoRSS.IOExceptionParsing", docString);
            Logging.logger().log(Level.SEVERE, message, e);
            throw new WWRuntimeException(message, e);
        }
    }

    private static String fixNamespaceQualification(String xmlString)
    {
        String lcaseString = xmlString.toLowerCase();
        StringBuffer qualifiers = new StringBuffer();

        if (lcaseString.contains("georss:") && !lcaseString.contains(GEORSS_URI))
        {
            qualifiers.append(" xmlns:georss=\"");
            qualifiers.append(GEORSS_URI);
            qualifiers.append("\"");
        }

        if (lcaseString.contains("gml:") && !lcaseString.contains(GML_URI))
        {
            qualifiers.append(" xmlns:gml=\"");
            qualifiers.append(GML_URI);
            qualifiers.append("\"");
        }

        if (qualifiers.length() > 0)
        {
            StringBuffer sb = new StringBuffer();
            sb.append("<wwdummyelement");
            sb.append(qualifiers);
            sb.append(">");
            sb.append(xmlString);
            sb.append("</wwdummyelement>");

            return sb.toString();
        }

        return xmlString;
    }

    private static String surroundWithNameSpaces(String docString)
    {
        StringBuffer sb = new StringBuffer();
        sb.append("<wwdummyelement");
        sb.append(" xmlns:georss=\"");
        sb.append(GEORSS_URI);
        sb.append("\"");
        sb.append(" xmlns:gml=\"");
        sb.append(GML_URI);
        sb.append("\"");
        sb.append(">");
        sb.append(docString);
        sb.append("</wwdummyelement>");
//        System.out.println(sb.toString());

        return sb.toString();
    }

    public static List<Renderable> parseShapes(File file)
    {
        if (file == null)
        {
            String message = Logging.getMessage("nullValue.FileIsNull");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }

        try
        {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            docBuilderFactory.setNamespaceAware(false);
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(file);

            List<Renderable> shapes = parseShapes(doc);

            if (shapes == null || shapes.size() < 1)
            {
                Logging.logger().log(Level.WARNING, "GeoRSS.NoShapes", file.getPath());
                return null;
            }

            return shapes;
        }
        catch (ParserConfigurationException e)
        {
            String message = Logging.getMessage("GeoRSS.ParserConfigurationException");
            Logging.logger().log(Level.SEVERE, message, e);
            throw new WWRuntimeException(message, e);
        }
        catch (IOException e)
        {
            String message = Logging.getMessage("GeoRSS.IOExceptionParsing", file.getPath());
            Logging.logger().log(Level.SEVERE, message, e);
            throw new WWRuntimeException(message, e);
        }
        catch (SAXException e)
        {
            String message = Logging.getMessage("GeoRSS.IOExceptionParsing", file.getPath());
            Logging.logger().log(Level.SEVERE, message, e);
            throw new WWRuntimeException(message, e);
        }
    }

    public static List<Renderable> parseShapes(Document xmlDoc)
    {
        if (xmlDoc == null)
        {
            String message = Logging.getMessage("nullValue.DocumentIsNull");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }

        ArrayList<Node> shapeNodes = new ArrayList<Node>();
        ArrayList<Node> attributeNodes = new ArrayList<Node>();

        // Shapes
        NodeList nodes = xmlDoc.getElementsByTagNameNS(GEORSS_URI, "where");
        if (nodes != null && nodes.getLength() > 0)
            addNodes(shapeNodes, nodes);

        nodes = xmlDoc.getElementsByTagNameNS(GEORSS_URI, "point");
        if (nodes != null && nodes.getLength() > 0)
            addNodes(shapeNodes, nodes);

        nodes = xmlDoc.getElementsByTagNameNS(GEORSS_URI, "line");
        if (nodes != null && nodes.getLength() > 0)
            addNodes(shapeNodes, nodes);

        nodes = xmlDoc.getElementsByTagNameNS(GEORSS_URI, "polygon");
        if (nodes != null && nodes.getLength() > 0)
            addNodes(shapeNodes, nodes);

        nodes = xmlDoc.getElementsByTagNameNS(GEORSS_URI, "box");
        if (nodes != null && nodes.getLength() > 0)
            addNodes(shapeNodes, nodes);

        // Attributes
        nodes = xmlDoc.getElementsByTagNameNS(GEORSS_URI, "radius");
        if (nodes != null && nodes.getLength() > 0)
            addNodes(attributeNodes, nodes);

        nodes = xmlDoc.getElementsByTagNameNS(GEORSS_URI, "elev");
        if (nodes != null && nodes.getLength() > 0)
            addNodes(attributeNodes, nodes);

        ArrayList<Renderable> shapes = new ArrayList<Renderable>();

        if (shapeNodes.size() < 1)
            return null; // No warning here. Let the calling method inform of this case.

        for (Node node : shapeNodes)
        {
            Renderable shape = null;
            String localName = node.getLocalName();

            if (localName.equals("point"))
                shape = makePointShape(node, attributeNodes);

            else if (localName.equals("where"))
                shape = makeWhereShape(node);

            else if (localName.equals("line"))
                shape = makeLineShape(node, attributeNodes);

            else if (localName.equals("polygon"))
                shape = makePolygonShape(node, attributeNodes);

            else if (localName.equals("box"))
                shape = makeBoxShape(node, attributeNodes);

            if (shape != null)
                shapes.add(shape);
        }

        return shapes;
    }

    private static void addNodes(ArrayList<Node> nodeList, NodeList nodes)
    {
        for (int i = 0; i < nodes.getLength(); i++)
        {
            nodeList.add(nodes.item(i));
        }
    }

    private static Renderable makeWhereShape(Node node)
    {
        Node typeNode = findChildByLocalName(node, "Polygon");
        if (typeNode != null)
            return makeGMLPolygonShape(typeNode);

        typeNode = findChildByLocalName(node, "Envelope");
        if (typeNode != null)
            return makeGMLEnvelopeShape(typeNode);

        typeNode = findChildByLocalName(node, "LineString");
        if (typeNode != null)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?