Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2007-2013 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is 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 * * Contributor: * Red Hat, Inc. - initial API and implementation ******************************************************************************/ import java.util.ArrayList; import java.util.List; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { private static final String ATTRIBUTE_NAME = "name"; private static final String TAG_JS_MODULE = "js-module"; private static final String TAG_PLATFORM = "platform"; private static List<Element> getJsModulesForSpecificPlatform(Document doc, String platformName) { List<Element> suitableJsModules = new ArrayList<Element>(); Element documentElement = doc.getDocumentElement(); NodeList childNodes = documentElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (isJsModuleElement(element)) { // Common js-module for all types of projects (ios, android, wp8 etc.) suitableJsModules.add(element); } else if (isPlatformElement(element) && (element.getAttribute(ATTRIBUTE_NAME) != null) && element.getAttribute(ATTRIBUTE_NAME).equals(platformName)) { // platform-specific js-module List<Element> androidJsModules = getChildElementsByName(element, TAG_JS_MODULE); suitableJsModules.addAll(androidJsModules); } } } return suitableJsModules; } private static boolean isJsModuleElement(Element element) { String elementName = element.getNodeName(); return elementName.equals(TAG_JS_MODULE); } private static boolean isPlatformElement(Element element) { String elementName = element.getNodeName(); return elementName.equals(TAG_PLATFORM); } private static List<Element> getChildElementsByName(Element parentElement, String childElementName) { List<Element> elementList = new ArrayList<Element>(); NodeList childNodes = parentElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) node; if (childElement.getNodeName().equals(childElementName)) { elementList.add(childElement); } } } return elementList; } }