001package org.vishia.xml;
002
003import java.util.Iterator;
004import java.util.LinkedList;
005import java.util.List;
006import java.util.Map;
007
008import org.jdom.Element;
009import org.jdom.Namespace;
010import org.vishia.xmlSimple.XmlException;
011import org.vishia.xmlSimple.XmlNode;
012
013/**This class wraps a JDOM Element with a implementation of {@link org.vishia.xmlSimple.XmlNode}.
014 * So some algorithm using XmlNode can execute with a given JDOM XML Tree. 
015 * <br>
016 * This class is used in 
017 * {@link org.vishia.xml.CorrectHref}. There a JDOM tree is used, 
018 * and {@link org.vishia.xmlSimple.WikistyleTextToSimpleXml} is called there. The last class 
019 * WikistyleTextToSimpleXml don't know JDOM, but it works correct.  
020 * 
021 * @author Hartmut Schorrig
022 *
023 */
024public class XmlNodeJdom implements XmlNode
025{
026
027  final Element jdomNode;
028
029  List<XmlNode> listChildren;
030  
031  public XmlNodeJdom(Element jdomNode)
032  { this.jdomNode =jdomNode;
033  }
034  
035  
036  public XmlNode addContent(String text)
037  {
038    jdomNode.addContent(text);
039    return this;
040  }
041
042  public XmlNode addContent(XmlNode node) throws XmlException
043  { if(node instanceof XmlNodeJdom)
044    {
045      jdomNode.addContent(((XmlNodeJdom)node).jdomNode);
046    }
047    else
048    { Element newNode = new Element(node.getName());
049      jdomNode.addContent(newNode);
050    }
051    return this;
052  }
053
054  public void addNamespaceDeclaration(String name, String value)
055  {
056    // TODO Auto-generated method stub
057    
058  }
059
060  public XmlNode addNewNode(String name, String namespaceKey) throws XmlException
061  {
062    XmlNode node = createNode(name,namespaceKey);
063    addContent(node);
064    return node;
065  }
066
067  public XmlNode createNode(String name, String namespaceKey) throws XmlException
068  { org.jdom.Element jdomNodeNew;
069    if(namespaceKey != null && namespaceKey.length()>0)
070    { Namespace ns = jdomNode.getNamespace(namespaceKey);
071      if(ns == null) throw new XmlException("unknown namespace: " + namespaceKey);
072      jdomNodeNew = new Element(name, ns);
073    }
074    else
075    { jdomNodeNew = new Element(name);
076    }
077    XmlNodeJdom node = new XmlNodeJdom(jdomNodeNew);  //the wrapper of the jdom node.
078    return node;
079  }
080
081  public String getAttribute(String name)
082  {
083    org.jdom.Attribute attrib = jdomNode.getAttribute(name);
084    if(attrib != null){ return attrib.getValue(); }
085    else return null;
086  }
087
088  public Map<String, String> getAttributes()
089  {
090    // TODO Auto-generated method stub
091    return null;
092  }
093
094  public XmlNode getChild(String key)
095  {
096    org.jdom.Element jdomChild = jdomNode.getChild(key);
097    return new XmlNodeJdom(jdomChild);
098  }
099
100  public String getName()
101  {
102    return jdomNode.getName();
103  }
104
105  public String getNamespaceKey()
106  {
107    return jdomNode.getNamespace().getPrefix();
108  }
109
110  public Map<String, String> getNamespaces()
111  {
112    return null;
113  }
114
115  public XmlNode getParent()
116  {
117    return new XmlNodeJdom(jdomNode.getParentElement());
118  }
119
120  public String text()
121  {
122    // TODO Auto-generated method stub
123    return jdomNode.getTextTrim();
124  }
125
126  public boolean isTextNode()
127  {
128    
129    return false;
130  }
131
132  public String removeAttribute(String name)
133  {
134    jdomNode.removeAttribute(name);
135    return null;
136  }
137
138  public void removeChildren()
139  {
140    jdomNode.removeContent();    
141  }
142
143  public void setAttribute(String name, String value)
144  {
145    jdomNode.setAttribute(new org.jdom.Attribute(name, value));
146    
147  }
148
149  public void setParent(XmlNode parent)
150  {
151    // TODO Auto-generated method stub
152    
153  }
154
155  public Iterator<XmlNode> iterChildren()
156  {
157    listChildren();
158    return listChildren.iterator();
159  }
160
161  public Iterator<XmlNode> iterChildren(String key)
162  {
163    return listChildren(key).iterator();
164  }
165
166  @SuppressWarnings("unchecked")
167  public List<XmlNode> listChildren()
168  {
169    if(listChildren == null)
170    { listChildren = new LinkedList<XmlNode>();
171      List<Element> jdomChildren = jdomNode.getChildren();
172      for(Element jdomChild : jdomChildren)
173      { listChildren.add(new XmlNodeJdom(jdomChild));
174      }
175    }
176    // TODO Auto-generated method stub
177    return listChildren;
178  }
179
180  @SuppressWarnings("unchecked")
181  public List<XmlNode> listChildren(String key)
182  {
183    List<XmlNode> listChildrenKey = new LinkedList<XmlNode>();
184    List<Element> jdomChildren = jdomNode.getChildren(key);
185    for(Element jdomChild : jdomChildren)
186    { listChildrenKey.add(new XmlNodeJdom(jdomChild));
187    }
188    // TODO Auto-generated method stub
189    return listChildrenKey;
190  }
191
192  
193  @Override
194  public String toString()
195  { return jdomNode.getName();
196  }
197  
198  
199}