Project: bml_lib License: BSD Dependencies: Used by: |
bml_lib/src/edu/wpi/hri/bml/behavior/EmitBehavior.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.bml.behavior; 00035 00036 import org.w3c.dom.Document; 00037 import org.w3c.dom.Element; 00038 import org.w3c.dom.NodeList; 00039 00040 import edu.wpi.hri.bml.behavior.BehaviorEnums.BehaviorType; 00041 import edu.wpi.hri.log.Logger; 00042 import edu.wpi.hri.log.Logger.LoggerLevel; 00043 00050 public class EmitBehavior extends Behavior { 00051 00052 private static final String EVENT_ELEMENT = "event"; 00053 private static final String SOURCE_ATTR = "source"; 00054 00055 private final SyncRef source; 00056 private final EventBehavior event; 00057 00066 public EmitBehavior(Logger logger, Element elem) { 00067 super(logger, elem); 00068 00069 // first check that the source exists 00070 if (elem.hasAttribute(SOURCE_ATTR)) 00071 this.source = new SyncRef(elem.getAttribute(SOURCE_ATTR)); 00072 else 00073 this.source = null; 00074 00075 // then get the event out 00076 NodeList list = elem.getElementsByTagName(EVENT_ELEMENT); 00077 if (list.getLength() > 0) 00078 this.event = new EventBehavior(logger, (Element) (list.item(0))); 00079 else 00080 this.event = new EventBehavior(logger, "", ""); 00081 00082 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00083 } 00084 00099 public EmitBehavior(Logger logger, String id, SyncRef source, 00100 EventBehavior event, boolean required) { 00101 super(logger, id, required); 00102 this.source = source; 00103 this.event = event; 00104 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00105 } 00106 00112 public SyncRef getSource() { 00113 return source; 00114 } 00115 00121 public EventBehavior getEvent() { 00122 return this.event; 00123 } 00124 00125 @Override 00126 protected void appendAttributes(Document doc, Element elem) { 00127 00128 // ensure that the source is valid 00129 if (this.source != null) 00130 elem.setAttribute(SOURCE_ATTR, this.source.toString()); 00131 00132 // then add the event 00133 elem.appendChild(this.event.toBML(doc)); 00134 } 00135 00136 @Override 00137 protected String getElementName() { 00138 return BehaviorType.EMIT.elementName; 00139 } 00140 00147 public static class EventBehavior extends Behavior { 00148 00149 private final String text; 00150 00159 public EventBehavior(Logger logger, Element elem) { 00160 super(logger, elem); 00161 this.text = elem.getTextContent(); 00162 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00163 } 00164 00175 public EventBehavior(Logger logger, String id, String text) { 00176 super(logger, id, false); 00177 this.text = text; 00178 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00179 } 00180 00186 public String getText() { 00187 return this.text; 00188 } 00189 00190 @Override 00191 protected void appendAttributes(Document doc, Element elem) { 00192 elem.setTextContent(this.text); 00193 } 00194 00195 @Override 00196 protected String getElementName() { 00197 return EVENT_ELEMENT; 00198 } 00199 } 00200 } |