/* * Copyright (c) 2005-2007, ASTcentric group, All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * Created on 02.06.2007 */ package astcentric.example.properties; import java.util.List; import java.util.Properties; import astcentric.structure.basic.AST; import astcentric.structure.basic.Node; import astcentric.structure.basic.NodeException; import astcentric.structure.basic.NodeHandler; import astcentric.structure.basic.NodeTool; import astcentric.structure.basic.id.ASTID; public class PropertiesGenerator { private static class PropertiesDeclaration { private static final ASTID PROPERTIES_DECLARATION_AST_ID = new ASTID("astcentric.sf.net", "fjelmer", 1); private final Node plainValueNode; private final Node valueReferenceNode; private final Node propertyNode; PropertiesDeclaration(AST ast) { if (ast.getInfo().getID().equals(PROPERTIES_DECLARATION_AST_ID) == false) { throw new IllegalArgumentException( "Properties Declaration AST expected instead of: " + ast); } List visibleNodes = NodeTool.collectNodesVisibleFor(ast, ast); plainValueNode = visibleNodes.get(1); valueReferenceNode = visibleNodes.get(2); propertyNode = visibleNodes.get(3); } } public static Properties generate(Node propertiesNode) { Node reference = propertiesNode.getReference(); if (reference == null) { throw new NodeException(propertiesNode, "Reference to Properties Declaration missing"); } AST ast = reference.getAST(); PropertiesDeclaration declaration = new PropertiesDeclaration(ast); Properties properties = new Properties(); List propNodes = NodeTool.getNodesOf(propertiesNode); for (Node propNode : propNodes) { String name = propNode.getName(); if (name == null) { throw new NodeException(propNode, "Nameless property node."); } properties.setProperty(name, getValue(propNode, declaration)); } return properties; } private static String getValue(Node node, final PropertiesDeclaration declaration) { Node reference = node.getReference(); if (reference == null) { throw new NodeException(node, "Missing reference."); } if (NodeTool.same(reference, declaration.plainValueNode)) { return node.getValue().toString(); } if (NodeTool.same(reference, declaration.valueReferenceNode)) { Node ref = NodeTool.getNodesOf(node).get(0).getReference(); return getValue(ref, declaration); } if (NodeTool.same(reference, declaration.propertyNode)) { final StringBuilder builder = new StringBuilder(); node.traverseNodes(new NodeHandler() { public boolean handle(Node child) { builder.append(getValue(child, declaration)); return false; } }); return new String(builder); } throw new NodeException(node, "Isn't declared correctly: " + reference); } private PropertiesGenerator() {} }