Project: engagement_generation License: BSD Dependencies:
Used by:
None |
engagement_generation/src/edu/wpi/hri/gen/ebml/EBMLList.javaGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 package edu.wpi.hri.gen.ebml; 00035 00036 import java.io.StringReader; 00037 import java.util.ArrayList; 00038 import java.util.List; 00039 00040 import org.w3c.dom.Document; 00041 import org.w3c.dom.Element; 00042 import org.w3c.dom.NodeList; 00043 00044 import edu.wpi.hri.bml.XMLInterface; 00045 import edu.wpi.hri.bml.behavior.Behavior; 00046 import edu.wpi.hri.bml.behavior.BehaviorList; 00047 import edu.wpi.hri.log.Logger; 00048 import edu.wpi.hri.log.Logger.LoggerLevel; 00049 00059 public class EBMLList extends BehaviorList { 00060 00061 private final List<ReferenceBehavior> references; 00062 00074 public EBMLList(Logger logger, XMLInterface xml, Element root) { 00075 super(logger, xml, root); 00076 00077 // remember each reference under the root node 00078 this.listLogger.debug(LoggerLevel.BEHAVIOR, "Loading references"); 00079 this.references = new ArrayList<ReferenceBehavior>(); 00080 // then for each sub element in the root that is good, parse it 00081 NodeList list = root.getElementsByTagName("reference"); 00082 for (int c = 0; c < list.getLength(); c++) 00083 this.references.add(new ReferenceBehavior(logger, (Element) list 00084 .item(c))); 00085 00086 // store the behaviors by id to retrieve them easily. 00087 for (Behavior b : this.references) 00088 map.put(b.getID(), b); 00089 00090 this.listLogger.debug(LoggerLevel.BEHAVIOR, "EBML loaded"); 00091 } 00092 00104 public EBMLList(Logger logger, XMLInterface xml, String id) { 00105 super(logger, xml, id); 00106 this.references = new ArrayList<ReferenceBehavior>(); 00107 } 00108 00114 public List<ReferenceBehavior> getReferences() { 00115 return references; 00116 } 00117 00124 public void removeReference(ReferenceBehavior ref) { 00125 this.references.remove(ref); 00126 this.map.remove(ref.getID()); 00127 } 00128 00129 @Override 00130 public List<Behavior> getAllBehaviors() { 00131 List<Behavior> all = super.getAllBehaviors(); 00132 all.addAll(this.references); 00133 return all; 00134 } 00135 00136 @Override 00137 public int size() { 00138 return super.size() + this.references.size(); 00139 } 00140 00157 public static EBMLList parseEBML(Logger logger, XMLInterface xml, 00158 String ebml) { 00159 Document doc = xml.loadDocument(new StringReader(ebml)); 00160 if (doc == null) 00161 return null; 00162 else 00163 return new EBMLList(logger, xml, doc.getDocumentElement()); 00164 } 00165 } |