001/* 002 * Copyright (c) 2004-2012, Willem Cazander 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided 006 * that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the 009 * following disclaimer. 010 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 011 * the following disclaimer in the documentation and/or other materials provided with the distribution. 012 * 013 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 015 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 016 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 017 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 018 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 019 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 020 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 021 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 022 */ 023 024package org.x4o.xml.test.swixml; 025 026import java.awt.Component; 027 028import javax.el.ValueExpression; 029 030import org.x4o.xml.core.X4OParser; 031import org.x4o.xml.core.config.X4OLanguagePropertyKeys; 032import org.x4o.xml.element.ElementLanguage; 033 034/** 035 * SwiXmlParser works with the SwingEngine to config the UI tree. 036 * 037 * @author Willem Cazander 038 * @version 1.0 Aug 15, 2012 039 */ 040public class SwiXmlParser extends X4OParser { 041 042 public static final String LANGUAGE = "swixml"; 043 public static final String VERSION_2_NS_URI = "http://swixml.x4o.org/xml/ns/swixml-lang"; 044 public static final String SWING_ENGINE_EL_NAME = "swingEngine"; 045 046 /** 047 * Protected constructor for write schema api. 048 * @param xmlVersion The xml version. 049 */ 050 protected SwiXmlParser(SwiXmlVersion xmlVersion) { 051 052 // Create language by version 053 super(LANGUAGE,xmlVersion.getLanguageVersion()); 054 055 // Sets empty namespace uri for version 2 xml documents 056 if (SwiXmlVersion.VERSION_2.equals(xmlVersion)) { 057 setProperty(X4OLanguagePropertyKeys.INPUT_EMPTY_NAMESPACE_URI, VERSION_2_NS_URI); 058 } 059 } 060 061 /** 062 * Creates an SwiXmlParser with an SwingEngine and a xml version. 063 * @param engine The SwingEngine to parse for. 064 * @param xmlVersion The xml language version to parse. 065 */ 066 public SwiXmlParser(SwingEngine engine,SwiXmlVersion xmlVersion) { 067 068 // Create language 069 this(xmlVersion); 070 071 // Check engine 072 if (engine==null) { 073 throw new NullPointerException("Can't parse with null SwingEngine."); 074 } 075 076 // Add engine for callback 077 addELBeanInstance(SWING_ENGINE_EL_NAME,engine); 078 } 079 080 /** 081 * Returns after parsing the root component. 082 * @return Returns the root component. 083 */ 084 public Component getRootComponent() { 085 return (Component)getDriver().getElementLanguage().getRootElement().getElementObject(); 086 } 087 088 /** 089 * Helper for while parsing to get the SwingEngine. 090 * @param elementLanguage The elementLanguage to get the swingEngine out. 091 * @return Returns the SwingEngine for this elementLanguage. 092 */ 093 static public SwingEngine getSwingEngine(ElementLanguage elementLanguage) { 094 ValueExpression ee = elementLanguage.getExpressionFactory().createValueExpression(elementLanguage.getELContext(),"${"+SwiXmlParser.SWING_ENGINE_EL_NAME+"}",Object.class); 095 SwingEngine se = (SwingEngine)ee.getValue(elementLanguage.getELContext()); 096 return se; 097 } 098 099 /** 100 * SwiXmlVersion defines the xml language version to parse. 101 */ 102 public enum SwiXmlVersion { 103 VERSION_2("2.0"), 104 VERSION_3("3.0"); 105 106 final private String languageVersion; 107 private SwiXmlVersion(String languageVersion) { 108 this.languageVersion=languageVersion; 109 } 110 public String getLanguageVersion() { 111 return languageVersion; 112 } 113 } 114}