Skip to main content
summaryrefslogtreecommitdiffstats
blob: 84c2f334b9e5b09dade0ddf030bb5bc522d57afe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*******************************************************************************
 * Copyright (c) 2013 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.help.ui.util;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Angel Avila
 */
public class TocParser {

   private static final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();

   private static final String TOC = "toc";
   private static final String TOPIC = "topic";
   private static final String HREF = "href";

   public Set<String> entries = new HashSet<>();
   public String path;

   private String localName;
   private String uri;

   public TocParser(String path) {
      this.path = path;
   }

   public void parse() throws Exception {
      entries.clear();
      URL url = HelpTestUtil.getResource(path);

      InputStream inputStream = null;
      try {
         inputStream = new BufferedInputStream(url.openStream());
         XMLStreamReader streamReader = xmlInputFactory.createXMLStreamReader(inputStream);
         while (streamReader.hasNext()) {
            process(streamReader);
            streamReader.next();
         }

      } finally {
         Lib.close(inputStream);
      }
   }

   private void process(XMLStreamReader reader) {
      int eventType = reader.getEventType();
      switch (eventType) {
         case XMLStreamConstants.START_ELEMENT:
            localName = reader.getLocalName();
            uri = reader.getNamespaceURI();
            if (TOC.equals(localName)) {
               processReference(reader, TOPIC);
            } else if (TOPIC.equals(localName)) {
               processReference(reader, HREF);
            }
            break;
         case XMLStreamConstants.END_ELEMENT:
            localName = reader.getLocalName();
            uri = reader.getNamespaceURI();
            reset();
            break;
      }
   }

   private void processReference(XMLStreamReader reader, String tag) {
      String reference = reader.getAttributeValue(uri, tag);
      if (Strings.isValid(reference)) {
         String path = normalizePath(reference);
         entries.add(path);
      }
   }

   private String normalizePath(String reference) {
      return reference.replaceAll("\\.html#.*", ".html");
   }

   private void reset() {
      localName = null;
      uri = null;
   }

   public Set<String> getEntries() {
      return entries;
   }
}

Back to the top