Enable word wrapping with preference key editor.wrap.words (false by default)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / connect / XMLReader.java
index 8cdfc20..5729e55 100644 (file)
@@ -30,8 +30,11 @@ import org.xml.sax.SAXParseException;
  */
 public class XMLReader {
   private static final String TITLE_TAG = "title"; //$NON-NLS-1$
+
   private static final String TIMESTAMP_TAG = "timestamp"; //$NON-NLS-1$
+
   private static final String TEXT_TAG = "text"; //$NON-NLS-1$
+
   private static final String PAGE_TAG = "page"; //$NON-NLS-1$
 
   public XMLReader() {
@@ -86,20 +89,20 @@ public class XMLReader {
   private static void traverse(String eleName, Node cNode, Parsed parsed) {
     switch (cNode.getNodeType()) {
     case Node.DOCUMENT_NODE:
-//      System.out.println("DOCUMENT_NODE " + cNode.getNodeName());
+      //      System.out.println("DOCUMENT_NODE " + cNode.getNodeName());
       processChildren(eleName, cNode.getChildNodes(), parsed);
       break;
 
     case Node.ELEMENT_NODE:
       eleName = cNode.getNodeName();
-//      System.out.println("ELEMENT_NODE " + eleName);
-//      NamedNodeMap attributeMap = cNode.getAttributes();
-//      int numAttrs = attributeMap.getLength();
-//      for (int i = 0; i < attributeMap.getLength(); i++) {
-//        Attr attribute = (Attr) attributeMap.item(i);
-//        String attrName = attribute.getNodeName();
-//        String attrValue = attribute.getNodeValue();
-//      }
+      //      System.out.println("ELEMENT_NODE " + eleName);
+      //      NamedNodeMap attributeMap = cNode.getAttributes();
+      //      int numAttrs = attributeMap.getLength();
+      //      for (int i = 0; i < attributeMap.getLength(); i++) {
+      //        Attr attribute = (Attr) attributeMap.item(i);
+      //        String attrName = attribute.getNodeName();
+      //        String attrValue = attribute.getNodeValue();
+      //      }
       processChildren(eleName, cNode.getChildNodes(), parsed);
       break;
     case Node.CDATA_SECTION_NODE:
@@ -127,10 +130,59 @@ public class XMLReader {
   }
 
   /**
+   * Read the first timestamp found in the Wikipedia xml stream
+   * 
+   * @param stream
+   * @return
+   * @throws Exception
+   */
+  public static String getTimestamp(InputStream stream) throws Exception {
+    //  Create a factory object for creating DOM parsers
+    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+    //  Now use the factory to create a DOM parser (a.k.a. a DocumentBuilder)
+    DocumentBuilder parser = factory.newDocumentBuilder();
+    //  Parse the file and build a Document tree to represent its content
+    Document document = parser.parse(stream);
+    //  Ask the document for a list of all <page> tags it contains
+    NodeList timestamps = document.getElementsByTagName(TIMESTAMP_TAG);
+    //  Loop through those <mediawiki> elements one at a time, and extract the
+    //  content of their <page> tags.
+    int numPages = timestamps.getLength();
+    for (int i = 0; i < numPages; i++) {
+      ElementNode page = (ElementNode) timestamps.item(i); // A <timestamp>
+      return page.getChildNodes().item(0).getNodeValue();
+    }
+    return null;
+  }
+  
+  /**
+   * Get the timestamp as java Date Format String
+   * 
+   * @param stream
+   * @return
+   * @throws Exception
+   */
+  public static String getDateTimestamp(InputStream stream) throws Exception {
+    String timestamp = getTimestamp(stream);
+    if (timestamp!=null) {
+      StringBuffer buffer = new StringBuffer();
+      // 2004-11-22T12:41:10Z
+      buffer.append(timestamp.substring(0,4));  //year
+      buffer.append(timestamp.substring(5,7));  //month
+      buffer.append(timestamp.substring(8,10)); //day
+      buffer.append(timestamp.substring(11,13));//hour
+      buffer.append(timestamp.substring(14,16));//minute
+      buffer.append(timestamp.substring(17,19));//second
+      return buffer.toString();
+    } 
+    return null;
+  }
+
+  /**
    * Reads the wikipedia xml data from the given stream
    * 
    * @param stream
-   * @return 
+   * @return
    * @throws CoreException
    */
   public static ArrayList readFromStream(Reader stream) throws CoreException {
@@ -144,8 +196,7 @@ public class XMLReader {
       Document document = parser.parse(new InputSource(stream));
       //  Ask the document for a list of all <page> tags it contains
       NodeList pages = document.getElementsByTagName(PAGE_TAG);
-      //  Loop through those <mediawiki> elements one at a time, and extract the
-      //  content of their <page> tags.
+      //  Loop through those <page> elements one at a time
       int numPages = pages.getLength();
 
       for (int i = 0; i < numPages; i++) {
@@ -162,7 +213,7 @@ public class XMLReader {
     } catch (IOException e) {
       throwReadException(e);
     } catch (SAXParseException e) {
-//      System.out.println("SAXParseException in line:" + e.getLineNumber() + " column:" + e.getColumnNumber());
+      //      System.out.println("SAXParseException in line:" + e.getLineNumber() + " column:" + e.getColumnNumber());
       throwReadException(e);
     } catch (SAXException e) {
       throwReadException(e);
@@ -174,7 +225,7 @@ public class XMLReader {
     Node node = attributes.getNamedItem(name);
     return node == null ? null : node.getNodeValue();
   }
-  
+
   //  public static void saveToFile(File file) throws CoreException {
   //    OutputStream stream = null;
   //    try {
@@ -265,7 +316,7 @@ public class XMLReader {
           + "    <title>Mechanisches Fernsehen</title>\r\n"
           + "    <revision>\r\n"
           + "      <timestamp>2004-11-22T12:41:10Z</timestamp>\r\n"
-          + "      <contributor><username>Electrocat</username></contributor>\r\n"
+          + "      <contributor><category>Electrocat</category></contributor>\r\n"
           + "      <text>Als \'\'\'mechanisches Fernsehen\'\'\' bezeichnet man [[Fernsehen]], bei dem die Bildzerlegung und -zusammensetzung im Gegensatz zum [[elektronisches Fernsehen|elektronischen Fernsehen]] mechanisch erfolgt. Die eigentliche Übertragung findet natürlich auf elektrischem Wege statt.\r\n"
           + "\r\n"
           + "Das mechanische Fernsehen war die erste Form des Fernsehens. Die erste  brauchbare Realisierung erfolgte mit Hilfe der nach ihrem Erfinder [[Paul Nipkow]] benannten [[Nipkow-Scheibe]]. Hierbei ist insbesondere die Pionierarbeit des ungarischen Ingenieurs [[D. von Mihaly]] und des schottischen Erfinders [[John Logie Baird]] zu erwähnen. [[D. von Mihaly]] entwickelte ebenfalls ein vollkommen anderes Verfahren, bei dem ein Spiegel zwischen einem Hufeisenmagneten schnell oszillierte. In einem verbesserten Verfahren wurde ein Spiegel auf Drahtsaiten befestigt, welche nach Stromdurchleitung in eine schnelle Schwingung versetzt wurden. Ein wieder anderes Verfahren entwickelte [[Dr. Carolus]] bei [[Telefunken]], wo mit schnell rotierenden Spiegeln gearbeitet wurde. Durch Carolus wurden auch beide Systeme miteinander kombiniert (Spiegelrad für horizontale Abtastung, oszillierende Spiegel für vertikale Abtastung. \r\n"
@@ -292,7 +343,7 @@ public class XMLReader {
           + "    <title>Anrechenbare Kosten</title>\r\n"
           + "    <revision>\r\n"
           + "      <timestamp>2004-09-22T17:18:23Z</timestamp>\r\n"
-          + "      <contributor><username>Fenice</username></contributor>\r\n"
+          + "      <contributor><category>Fenice</category></contributor>\r\n"
           + "      <comment>cat</comment>\r\n"
           + "\r\n"
           + "      <text>Die \'\'\'anrechenbaren Kosten\'\'\' sind eines der Regelkriterien bei der Ermittlung des [[Honorar]]s von Architekten und Ingenieuren nach der [[HOAI]]. Sie werden aus einem fachspezifischen Kostenanteil auf Basis der Kostenermittlungen nach [[DIN 276]] errechnet und können daher innerhalb eines Projektes je nach [[Leistungsphasen|Leistungsphase]] unterschiedlich hoch sein.\r\n"
@@ -347,9 +398,9 @@ public class XMLReader {
           + "\r\n" + "[[Kategorie:Rhetorischer Begriff]]\r\n"
           + "[[en:Synaesthesia]] [[es:Sinestesia]] [[sv:Synestesi]] [[tr:Sinestezi]]</text>\r\n" + "    </revision>\r\n"
           + "  </page>\r\n" + "</mediawiki>";
-      StringReader st = new StringReader(test2);
+      StringReader st = new StringReader(test);
 
-      readFromStream(st);
+      System.out.println(readFromStream(st));
     } catch (CoreException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();