Java XML NodeList getMetaScriptType(List metaNodeList)

Here you can find the source of getMetaScriptType(List metaNodeList)

Description

get Meta Script Type

License

Open Source License

Declaration

private static String getMetaScriptType(List metaNodeList) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2001, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  ww w .j  a  v  a 2s  . c  o m*/
 *     IBM Corporation - initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/

import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    public static final String CONTENT = "Content";
    public static final String CONTENT_SCRIPT_TYPE = "Content-Script-Type";
    public static final String HEAD = "HEAD";
    public static final String HTML = "HTML";
    public static final String HTTP_EQUIV = "HTTP-EQUIV";
    public static final String META = "META";

    private static String getMetaScriptType(Document doc) {
        // Can not just do a Document.getElementsByTagName(String) as this
        // needs
        // to be relatively fast.
        List metas = new ArrayList();
        // check for META tags under the Document
        Node html = null;
        Node head = null;
        Node child = null;
        // ----------------------------------------------------------------------
        // (pa) 20021217
        // cmvc defect 235554
        // performance enhancement: using child.getNextSibling() rather than
        // nodeList(item) for O(n) vs. O(n*n)
        // ----------------------------------------------------------------------

        for (child = doc.getFirstChild(); child != null; child = child
                .getNextSibling()) {
            if (child.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            if (child.getNodeName().equalsIgnoreCase(META)) {
                metas.add(child);
            } else if (child.getNodeName().equalsIgnoreCase(HTML)) {
                html = child;
            }
        }
        // NodeList children = doc.getChildNodes();
        // for(int i = 0; i < children.getLength(); i++) {
        // child = children.item(i);
        // if(child.getNodeType() != Node.ELEMENT_NODE)
        // continue;
        // if(child.getNodeName().equalsIgnoreCase(META))
        // metas.add(child);
        // else if(child.getNodeName().equalsIgnoreCase(HTML))
        // html = child;
        // }

        // check for META tags under HEAD
        if (html != null) {
            for (child = html.getFirstChild(); (child != null)
                    && (head == null); child = child.getNextSibling()) {
                if (child.getNodeType() != Node.ELEMENT_NODE) {
                    continue;
                }
                if (child.getNodeName().equalsIgnoreCase(HEAD)) {
                    head = child;
                }
            }
            // children = html.getChildNodes();
            // for(int i = 0; i < children.getLength() && head == null; i++) {
            // child = children.item(i);
            // if(child.getNodeType() != Node.ELEMENT_NODE)
            // continue;
            // if(child.getNodeName().equalsIgnoreCase(HEAD))
            // head = child;
            // }
        }

        if (head != null) {
            for (head.getFirstChild(); child != null; child = child
                    .getNextSibling()) {
                if (child.getNodeType() != Node.ELEMENT_NODE) {
                    continue;
                }
                if (child.getNodeName().equalsIgnoreCase(META)) {
                    metas.add(child);
                }
            }
            // children = head.getChildNodes();
            // for(int i = 0 ; i < children.getLength(); i++) {
            // child = children.item(i);
            // if(child.getNodeType() != Node.ELEMENT_NODE)
            // continue;
            // if(child.getNodeName().equalsIgnoreCase(META))
            // metas.add(child);
            // }
        }

        return getMetaScriptType(metas);
    }

    private static String getMetaScriptType(List metaNodeList) {
        Node meta = null;
        NamedNodeMap attributes = null;
        boolean httpEquiv = false;
        String contentScriptType = null;

        for (int i = metaNodeList.size() - 1; i >= 0; i--) {
            meta = (Node) metaNodeList.get(i);
            attributes = meta.getAttributes();
            httpEquiv = false;
            contentScriptType = null;
            for (int j = 0; j < attributes.getLength(); j++) {
                if (attributes.item(j).getNodeName()
                        .equalsIgnoreCase(HTTP_EQUIV)) {
                    httpEquiv = attributes.item(j).getNodeValue()
                            .equalsIgnoreCase(CONTENT_SCRIPT_TYPE);
                } else if (attributes.item(j).getNodeName()
                        .equalsIgnoreCase(CONTENT)) {
                    contentScriptType = attributes.item(j).getNodeValue();
                }
            }
            if (httpEquiv && (contentScriptType != null)) {
                return contentScriptType;
            }
        }
        return null;
    }
}

Related

  1. GetFirstInstance(NodeList nList)
  2. getFirstNode(NodeList nodeList)
  3. getFirstNodeValue(NodeList nodeList)
  4. getFirstTextValueFromNodeList(NodeList nList)
  5. getFirstValue(NodeList nodes)
  6. getMethodNames(NodeList methodNodes)
  7. getNode(NodeList nodes, String tagName)
  8. getNode(String tagName, NodeList nodes)
  9. getNodeByNodeName(NodeList nodes, String nodeName)