View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  package com.ontotext.wsmo4j.parser.xml;
19  
20  /**
21   * <p>Title: WSMO4J</p>
22   * <p>Description: WSMO API and a Reference Implementation</p>
23   * <p>Copyright:  Copyright (c) 2004-2005</p>
24   * <p>Company: OntoText Lab. / SIRMA </p>
25   * @author not attributable
26   * @version 1.0
27   *
28   */
29  
30  import org.w3c.dom.Node;
31  
32  /**
33   * Helper type to handle common xml routines
34   * 
35   * @author not attributable
36   */
37  class WsmlXmlHelper {
38      static Node getFirstChildElement(Node node) {
39          if (node == null) {
40              throw new IllegalArgumentException();
41          }
42  
43          Node result = node.getFirstChild();
44          while (result != null) {
45              if (result.getNodeType() == Node.ELEMENT_NODE) {
46                  return result;
47              }
48              result = result.getNextSibling();
49          }
50          return null;
51      }
52  
53      static String getAttrValue(Node node, String attrName) {
54          if (node == null || attrName == null || node.getNodeType() != Node.ELEMENT_NODE) {
55              throw new IllegalArgumentException();
56          }
57          
58          try {
59              return node.getAttributes().getNamedItem(attrName).getNodeValue().trim();
60          }
61          catch (NullPointerException e) {
62              throw new RuntimeException("Could not find a mandatory attribute [" + 
63                      attrName + "] for the node: " + node.toString());
64          }
65      }
66  
67      static String getElementText(Node node) {
68          if (node == null || node.getNodeType() != Node.ELEMENT_NODE) {
69              throw new IllegalArgumentException();
70          }
71          Node child = node.getFirstChild();
72          if (child == null) {
73              return "";
74          }
75          if (child.getNodeType() == Node.TEXT_NODE) {
76              return child.getNodeValue().trim();
77          }
78          throw new IllegalArgumentException();
79      }
80  }
81  
82  /*
83   * $Log$
84   * Revision 1.2  2006/03/29 11:20:51  vassil_momtchev
85   * mediator support added; some code refactored; minor bugs fixed
86   *
87   * Revision 1.1  2005/11/28 14:03:48  vassil_momtchev
88   * package refactored from com.ontotext.wsmo4j.xmlparser to com.ontotext.wsmo4j.parser.xml
89   *
90   * Revision 1.2  2005/08/08 08:24:40  vassil_momtchev
91   * javadoc added, bugfixes
92   *
93  */