unfinished release guide. It would be nice to have a html version.
[poi.git] / src / scratchpad / ooxml-src / org / apache / poi / hxf / dev / HXFLister.java
blob032b74b6f16328fdf05ab3f707d48360279075d6
1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
17 package org.apache.poi.hxf.dev;
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.PrintStream;
23 import java.util.ArrayList;
25 import org.openxml4j.opc.Package;
26 import org.openxml4j.opc.PackageAccess;
27 import org.openxml4j.opc.PackagePart;
28 import org.openxml4j.opc.PackageRelationship;
29 import org.openxml4j.opc.PackageRelationshipCollection;
31 /**
32 * Prints out the contents of a HXF (ooxml) container.
33 * Useful for seeing what parts are defined, and how
34 * they're all related to each other.
36 public class HXFLister {
37 private Package container;
38 private PrintStream disp;
40 public HXFLister(Package container) {
41 this(container, System.out);
43 public HXFLister(Package container, PrintStream disp) {
44 this.container = container;
45 this.disp = disp;
48 /**
49 * Figures out how big a given PackagePart is.
51 public static long getSize(PackagePart part) throws IOException {
52 InputStream in = part.getInputStream();
53 byte[] b = new byte[8192];
54 long size = 0;
55 int read = 0;
57 while(read > -1) {
58 read = in.read(b);
59 if(read > 0) {
60 size += read;
64 return size;
67 /**
68 * Displays information on all the different
69 * parts of the OOXML file container.
71 public void displayParts() throws Exception {
72 ArrayList<PackagePart> parts = container.getParts();
73 for (PackagePart part : parts) {
74 disp.println(part.getPartName());
75 disp.println("\t" + part.getContentType());
77 if(! part.getPartName().toString().equals("/docProps/core.xml")) {
78 disp.println("\t" + getSize(part) + " bytes");
81 if(! part.isRelationshipPart()) {
82 disp.println("\t" + part.getRelationships().size() + " relations");
83 for(PackageRelationship rel : part.getRelationships()) {
84 displayRelation(rel, "\t ");
89 /**
90 * Displays information on all the different
91 * relationships between different parts
92 * of the OOXML file container.
94 public void displayRelations() throws Exception {
95 PackageRelationshipCollection rels =
96 container.getRelationships();
97 for (PackageRelationship rel : rels) {
98 displayRelation(rel, "");
101 private void displayRelation(PackageRelationship rel, String indent) {
102 disp.println(indent+"Relationship:");
103 disp.println(indent+"\tFrom: "+ rel.getSourceURI());
104 disp.println(indent+"\tTo: " + rel.getTargetURI());
105 disp.println(indent+"\tID: " + rel.getId());
106 disp.println(indent+"\tMode: " + rel.getTargetMode());
107 disp.println(indent+"\tType: " + rel.getRelationshipType());
110 public static void main(String[] args) throws Exception {
111 if(args.length == 0) {
112 System.err.println("Use:");
113 System.err.println("\tjava HXFLister <filename>");
114 System.exit(1);
117 File f = new File(args[0]);
118 if(! f.exists()) {
119 System.err.println("Error, file not found!");
120 System.err.println("\t" + f.toString());
121 System.exit(2);
124 HXFLister lister = new HXFLister(
125 Package.open(f.toString(), PackageAccess.READ)
128 lister.disp.println(f.toString() + "\n");
129 lister.displayParts();
130 lister.disp.println();
131 lister.displayRelations();