📄 simplebookmark.java
字号:
for (Iterator it = kids.listIterator(); it.hasNext(); ++ptr) {
HashMap map = (HashMap)it.next();
Object lower[] = null;
List subKid = (List)map.get("Kids");
if (subKid != null && !subKid.isEmpty())
lower = iterateOutlines(writer, refs[ptr], subKid, namedAsNames);
PdfDictionary outline = new PdfDictionary();
++count;
if (lower != null) {
outline.put(PdfName.FIRST, (PdfIndirectReference)lower[0]);
outline.put(PdfName.LAST, (PdfIndirectReference)lower[1]);
int n = ((Integer)lower[2]).intValue();
if ("false".equals(map.get("Open"))) {
outline.put(PdfName.COUNT, new PdfNumber(-n));
}
else {
outline.put(PdfName.COUNT, new PdfNumber(n));
count += n;
}
}
outline.put(PdfName.PARENT, parent);
if (ptr > 0)
outline.put(PdfName.PREV, refs[ptr - 1]);
if (ptr < refs.length - 1)
outline.put(PdfName.NEXT, refs[ptr + 1]);
outline.put(PdfName.TITLE, new PdfString((String)map.get("Title"), PdfObject.TEXT_UNICODE));
String color = (String)map.get("Color");
if (color != null) {
try {
PdfArray arr = new PdfArray();
StringTokenizer tk = new StringTokenizer(color);
for (int k = 0; k < 3; ++k) {
float f = Float.parseFloat(tk.nextToken());
if (f < 0) f = 0;
if (f > 1) f = 1;
arr.add(new PdfNumber(f));
}
outline.put(PdfName.C, arr);
} catch(Exception e){} //in case it's malformed
}
String style = (String)map.get("Style");
if (style != null) {
style = style.toLowerCase();
int bits = 0;
if (style.indexOf("italic") >= 0)
bits |= 1;
if (style.indexOf("bold") >= 0)
bits |= 2;
if (bits != 0)
outline.put(PdfName.F, new PdfNumber(bits));
}
createOutlineAction(outline, map, writer, namedAsNames);
writer.addToBody(outline, refs[ptr]);
}
return new Object[]{refs[0], refs[refs.length - 1], new Integer(count)};
}
/**
* Exports the bookmarks to XML. Only of use if the generation is to be include in
* some other XML document.
* @param list the bookmarks
* @param out the export destination. The writer is not closed
* @param indent the indentation level. Pretty printing significant only
* @param onlyASCII codes above 127 will always be escaped with &#nn; if <CODE>true</CODE>,
* whatever the encoding
* @throws IOException on error
*/
public static void exportToXMLNode(List list, Writer out, int indent, boolean onlyASCII) throws IOException {
String dep = "";
for (int k = 0; k < indent; ++k)
dep += " ";
for (Iterator it = list.iterator(); it.hasNext();) {
HashMap map = (HashMap)it.next();
String title = null;
out.write(dep);
out.write("<Title ");
List kids = null;
for (Iterator e = map.entrySet().iterator(); e.hasNext();) {
Map.Entry entry = (Map.Entry) e.next();
String key = (String) entry.getKey();
if (key.equals("Title")) {
title = (String) entry.getValue();
continue;
}
else if (key.equals("Kids")) {
kids = (List) entry.getValue();
continue;
}
else {
out.write(key);
out.write("=\"");
String value = (String) entry.getValue();
if (key.equals("Named") || key.equals("NamedN"))
value = SimpleNamedDestination.escapeBinaryString(value);
out.write(SimpleXMLParser.escapeXML(value, onlyASCII));
out.write("\" ");
}
}
out.write(">");
if (title == null)
title = "";
out.write(SimpleXMLParser.escapeXML(title, onlyASCII));
if (kids != null) {
out.write("\n");
exportToXMLNode(kids, out, indent + 1, onlyASCII);
out.write(dep);
}
out.write("</Title>\n");
}
}
/**
* Exports the bookmarks to XML. The DTD for this XML is:
* <p>
* <pre>
* <?xml version='1.0' encoding='UTF-8'?>
* <!ELEMENT Title (#PCDATA|Title)*>
* <!ATTLIST Title
* Action CDATA #IMPLIED
* Open CDATA #IMPLIED
* Page CDATA #IMPLIED
* URI CDATA #IMPLIED
* File CDATA #IMPLIED
* Named CDATA #IMPLIED
* NamedN CDATA #IMPLIED
* NewWindow CDATA #IMPLIED
* Style CDATA #IMPLIED
* Color CDATA #IMPLIED
* >
* <!ELEMENT Bookmark (Title)*>
* </pre>
* @param list the bookmarks
* @param out the export destination. The stream is not closed
* @param encoding the encoding according to IANA conventions
* @param onlyASCII codes above 127 will always be escaped with &#nn; if <CODE>true</CODE>,
* whatever the encoding
* @throws IOException on error
*/
public static void exportToXML(List list, OutputStream out, String encoding, boolean onlyASCII) throws IOException {
String jenc = IanaEncodings.getJavaEncoding(encoding);
Writer wrt = new BufferedWriter(new OutputStreamWriter(out, jenc));
exportToXML(list, wrt, encoding, onlyASCII);
}
/**
* Exports the bookmarks to XML.
* @param list the bookmarks
* @param wrt the export destination. The writer is not closed
* @param encoding the encoding according to IANA conventions
* @param onlyASCII codes above 127 will always be escaped with &#nn; if <CODE>true</CODE>,
* whatever the encoding
* @throws IOException on error
*/
public static void exportToXML(List list, Writer wrt, String encoding, boolean onlyASCII) throws IOException {
wrt.write("<?xml version=\"1.0\" encoding=\"");
wrt.write(SimpleXMLParser.escapeXML(encoding, onlyASCII));
wrt.write("\"?>\n<Bookmark>\n");
exportToXMLNode(list, wrt, 1, onlyASCII);
wrt.write("</Bookmark>\n");
wrt.flush();
}
/**
* Import the bookmarks from XML.
* @param in the XML source. The stream is not closed
* @throws IOException on error
* @return the bookmarks
*/
public static List importFromXML(InputStream in) throws IOException {
SimpleBookmark book = new SimpleBookmark();
SimpleXMLParser.parse(book, in);
return book.topList;
}
/**
* Import the bookmarks from XML.
* @param in the XML source. The reader is not closed
* @throws IOException on error
* @return the bookmarks
*/
public static List importFromXML(Reader in) throws IOException {
SimpleBookmark book = new SimpleBookmark();
SimpleXMLParser.parse(book, in);
return book.topList;
}
public void endDocument() {
}
public void endElement(String tag) {
if (tag.equals("Bookmark")) {
if (attr.isEmpty())
return;
else
throw new RuntimeException("Bookmark end tag out of place.");
}
if (!tag.equals("Title"))
throw new RuntimeException("Invalid end tag - " + tag);
HashMap attributes = (HashMap)attr.pop();
String title = (String)attributes.get("Title");
attributes.put("Title", title.trim());
String named = (String)attributes.get("Named");
if (named != null)
attributes.put("Named", SimpleNamedDestination.unEscapeBinaryString(named));
named = (String)attributes.get("NamedN");
if (named != null)
attributes.put("NamedN", SimpleNamedDestination.unEscapeBinaryString(named));
if (attr.isEmpty())
topList.add(attributes);
else {
HashMap parent = (HashMap)attr.peek();
List kids = (List)parent.get("Kids");
if (kids == null) {
kids = new ArrayList();
parent.put("Kids", kids);
}
kids.add(attributes);
}
}
public void startDocument() {
}
public void startElement(String tag, HashMap h) {
if (topList == null) {
if (tag.equals("Bookmark")) {
topList = new ArrayList();
return;
}
else
throw new RuntimeException("Root element is not Bookmark: " + tag);
}
if (!tag.equals("Title"))
throw new RuntimeException("Tag " + tag + " not allowed.");
HashMap attributes = new HashMap(h);
attributes.put("Title", "");
attributes.remove("Kids");
attr.push(attributes);
}
public void text(String str) {
if (attr.isEmpty())
return;
HashMap attributes = (HashMap)attr.peek();
String title = (String)attributes.get("Title");
title += str;
attributes.put("Title", title);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -