/
DomToc.java
143 lines (133 loc) · 4.07 KB
/
DomToc.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2005--2008 Helsinki Institute for Information Technology
*
* This file is a part of Fuego middleware. Fuego middleware is free
* software; you can redistribute it and/or modify it under the terms
* of the MIT license, included as the file MIT-LICENSE in the Fuego
* middleware source distribution. If you did not receive the MIT
* license with the distribution, write to the Fuego Core project at
* fuego-xas-users@hoslab.cs.helsinki.fi.
*/
package fc.exper;
import java.io.FileOutputStream;
import java.util.Stack;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import fc.util.Measurer;
import fc.util.Util;
public class DomToc {
public static void main (String[] args) {
try {
if (args.length != 1) {
System.err.println("Usage: DomToc <file>");
System.exit(1);
}
Measurer.init(Measurer.TIMING);
Measurer timer = Measurer.get(Measurer.TIMING);
String fileName = args[0];
String outName = fileName.concat(".dtoc");
int end = 10;
for (int i = 0; i < end; i++ ) {
Util.runGc();
long beginMemory = Util.usedMemory();
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Object token = timer.start();
Document document = builder.parse(fileName);
Element tocNode = null;
Element chapter = null;
Element section = null;
Stack<Node> stack = new Stack<Node>();
stack.push(document);
while (!stack.isEmpty()) {
Node node = stack.pop();
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE: {
NodeList nodes = node.getChildNodes();
if (nodes != null) {
int n = nodes.getLength();
for (int j = n - 1; j >= 0; j-- ) {
stack.push(nodes.item(j));
}
}
break;
}
case Node.ELEMENT_NODE: {
String name = node.getNodeName();
Element element = (Element) node;
if (name.equals("toc")) {
tocNode = element;
} else if (name.equals("chapter")) {
String id = element.getAttribute("id");
if (id != null) {
chapter = document.createElement("tocchap");
chapter.setAttribute("ref", "#" + id);
tocNode.appendChild(chapter);
}
} else if (name.equals("sect1")) {
String id = element.getAttribute("id");
if (id != null) {
section = document.createElement("tocsect1");
section.setAttribute("ref", "#" + id);
chapter.appendChild(section);
}
} else if (name.equals("sect2")) {
String id = element.getAttribute("id");
if (id != null) {
Element subsection = document
.createElement("tocsect2");
subsection.setAttribute("ref", "#" + id);
section.appendChild(subsection);
}
break;
}
NodeList nodes = node.getChildNodes();
if (nodes != null) {
int n = nodes.getLength();
for (int j = n - 1; j >= 0; j-- ) {
stack.push(nodes.item(j));
}
}
break;
}
}
}
if (i >= end - 2) {
timer.finish(token, "TOC construction");
}
long endMemory = Util.usedMemory();
System.out.println("TOC memory: " + (endMemory - beginMemory));
Util.runGc();
endMemory = Util.usedMemory();
System.out.println("Document size: " + (endMemory - beginMemory));
Util.runGc();
beginMemory = Util.usedMemory();
token = timer.start();
OutputFormat format = new OutputFormat();
XMLSerializer ser = new XMLSerializer(new FileOutputStream(
outName), format);
ser.serialize(document);
if (i >= end - 2) {
timer.finish(token, "Output");
}
endMemory = Util.usedMemory();
System.out.println("Output memory: " + (endMemory - beginMemory));
if (i >= end - 2) {
timer.output(System.out);
}
}
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
}
// arch-tag: 98a1fa5e-d570-42cd-9a85-06af49054626