1 /* 2 wsmo4j - a WSMO API and Reference Implementation 3 Copyright (c) 2005, University of Innsbruck, Austria 4 This library is free software; you can redistribute it and/or modify it under 5 the terms of the GNU Lesser General Public License as published by the Free 6 Software Foundation; either version 2.1 of the License, or (at your option) 7 any later version. 8 This library is distributed in the hope that it will be useful, but WITHOUT 9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 10 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 11 details. 12 You should have received a copy of the GNU Lesser General Public License along 13 with this library; if not, write to the Free Software Foundation, Inc., 14 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 15 */ 16 package org.deri.wsmo4j.io.serializer.xml; 17 18 19 import java.util.*; 20 21 import org.w3c.dom.*; 22 23 import org.omwg.logicalexpression.*; 24 import org.omwg.logicalexpression.terms.*; 25 import org.omwg.logicalexpression.terms.Visitor; 26 import org.omwg.ontology.*; 27 import org.wsmo.common.*; 28 29 30 /** 31 * Concrete Visitor class. For each visited term, a document element is built. 32 * @see org.deri.wsmo4j.io.serializer.xml.VisitorSerializeXML 33 * @see org.omwg.logicalexpression.terms.Visitor 34 */ 35 public class VisitorSerializeXMLTerms 36 implements Visitor { 37 38 private Vector stack; 39 40 private Document doc; 41 42 private String name; 43 44 /** 45 * @param doc Document that will be filled with the xml structure 46 * @see org.deri.wsmo4j.io.serializer.xml.VisitorSerializeXML#VisitorSerializeXML(TopEntity, Document) 47 */ 48 public VisitorSerializeXMLTerms(Document doc) { 49 this.doc = doc; 50 stack = new Vector(); 51 name = "term"; 52 } 53 54 /** 55 * Set the type of element that is to be added to the document 56 * (term, atom or molecule). 57 * @param name type of element 58 */ 59 public void setName(String name) { 60 this.name = name; 61 } 62 63 /** 64 * Builds an element in the document with the ConstructedTerm's name 65 * as attribute. The arguments are added as children to the node. 66 * The element is then added to a vector. 67 * @param t ConstructedTerm to be serialized 68 * @see org.omwg.logicalexpression.terms.Visitor#visitConstructedTerm(org.omwg.logicalexpression.terms.ConstructedTerm) 69 */ 70 public void visitConstructedTerm(ConstructedTerm t) { 71 Element term = doc.createElement(name); 72 term.setAttribute("name", t.getFunctionSymbol().toString()); 73 74 int nbParams = t.getArity(); 75 for (int i = 0; i < nbParams; i++) { 76 name = "arg"; 77 t.getParameter(i).accept(this); 78 term.appendChild((Node)stack.remove(stack.size() - 1)); 79 } 80 stack.add(term); 81 } 82 83 /** 84 * Builds an element in the document with the Variable's String 85 * representation as attribute. The element is added to a vector. 86 * @param t Variable to be serialized 87 * @see org.omwg.logicalexpression.terms.Visitor#visitVariable(org.omwg.logicalexpression.terms.Variable) 88 */ 89 public void visitVariable(Variable t) { 90 Element term = doc.createElement(name); 91 term.setAttribute("name", Constants.VARIABLE_DEL + t.getName()); 92 stack.add(term); 93 } 94 95 public void visitComplexDataValue(ComplexDataValue t) { 96 Element term = doc.createElement(name); 97 term.setAttribute("name", t.getType().getIRI().toString()); 98 99 int nbParams = t.getArity(); 100 for (byte i = 0; i < nbParams; i++) { 101 name = "arg"; 102 ((Term)t.getArgumentValue(i)).accept(this); 103 term.appendChild((Node)stack.remove(stack.size() - 1)); 104 } 105 stack.add(term); 106 } 107 108 public void visitSimpleDataValue(SimpleDataValue t) { 109 Element value = doc.createElement(name); 110 value.setAttribute("name", t.getType().getIRI().toString()); 111 Text numVal = doc.createTextNode("" + t.getValue()); 112 value.appendChild(numVal); 113 stack.add(value); 114 } 115 116 /** 117 * Builds an element in the document with the Unnumbered 118 * Anonymous ID's String representation as attribute. 119 * The element is added to a vector. 120 * @param t UnNBAnonymousID to be serialized 121 * @see org.omwg.logicalexpression.terms.Visitor#visitAnonymousID(org.omwg.logicalexpression.terms.UnNbAnonymousID) 122 */ 123 public void visitUnnumberedID(UnnumberedAnonymousID t) { 124 Element term = doc.createElement(name); 125 term.setAttribute("name", Constants.ANONYMOUS_ID); 126 stack.add(term); 127 } 128 129 /** 130 * Builds an element in the document with the Numbered 131 * Anonymous ID's String representation as attribute. 132 * The element is added to a vector. 133 * @param t NbAnonymousID to be serialized 134 * @see org.omwg.logicalexpression.terms.Visitor#visitNbAnonymousID(org.omwg.logicalexpression.terms.NbAnonymousID) 135 */ 136 public void visitNumberedID(NumberedAnonymousID t) { 137 Element term = doc.createElement(name); 138 term.setAttribute("name", Constants.ANONYMOUS_ID + t.getNumber()); 139 stack.add(term); 140 } 141 142 /** 143 * Builds an element in the document with the IRI's String 144 * representation as attribute. The element is added to a vector. 145 * @param t IRI to be serialized 146 * @see org.omwg.logicalexpression.terms.Visitor#visitIRI(org.omwg.logicalexpression.terms.IRI) 147 */ 148 public void visitIRI(IRI t) { 149 Element term = doc.createElement(name); 150 term.setAttribute("name", t.toString()); 151 stack.add(term); 152 } 153 154 /** 155 * All serialized elements are added to a vector. This method removes the 156 * first serialized element from this vector and shifts any subsequent 157 * elements to the left (subtracts one from their indices). 158 * @return the serialized document node that is the first element in this vector 159 */ 160 public Object getSerializedObject() { 161 return stack.remove(0); 162 } 163 }