001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/pdfdoctools/DocManager.java,v 1.1 2005/04/20 21:04:36 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.pdfdoctools;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.Reader;
023: import java.io.BufferedReader;
024: import java.io.FileReader;
025: import java.io.StreamTokenizer;
026: import java.util.HashMap;
027: import java.net.URL;
028:
029: import org.jdesktop.j3dfly.namecontrol.NameControl;
030:
031: /**
032: * Manages all the document pages for current j3f file
033: *
034: * @author paulby
035: * @version
036: */
037: public class DocManager extends java.lang.Object {
038:
039: private HashMap objectMap;
040: private String documentPath = null;
041: private String documentURL;
042: private DocPanel docPanel;
043: private String defaultURL;
044:
045: /** Creates new DocManager */
046: public DocManager(DocPanel docPanel) {
047: this .docPanel = docPanel;
048: objectMap = new HashMap();
049: }
050:
051: /**
052: * Return the pdf bookmark location for the object
053: */
054: public String getBookmark(Object obj) {
055: String bookmark = (String) objectMap.get(obj);
056: if (bookmark == null)
057: bookmark = defaultURL;
058:
059: return bookmark;
060: }
061:
062: /**
063: * Load the index file
064: */
065: public void loadIndex(File file) {
066: String currentObject = null;
067: String currentScene = null;
068: documentPath = file.getAbsolutePath();
069: documentPath = documentPath.substring(0, documentPath
070: .lastIndexOf(File.separatorChar));
071:
072: try {
073: Reader reader = new BufferedReader(new FileReader(file));
074: StreamTokenizer tok = new StreamTokenizer(reader);
075:
076: while (tok.nextToken() != StreamTokenizer.TT_EOF) {
077: if (tok.ttype != StreamTokenizer.TT_WORD)
078: throw new IOException("Syntax Error at line "
079: + tok.lineno() + " " + tok);
080:
081: if (tok.sval.equals("object")) {
082: checkForEquals(tok);
083: System.out.println("Object " + tok.sval);
084: currentObject = tok.sval;
085: } else if (tok.sval.equals("document")) {
086: checkForEquals(tok);
087: System.out.println("Document " + tok.sval);
088: documentURL = tok.sval;
089: docPanel.setDocument(documentURL);
090: } else if (tok.sval.equals("doc")) {
091: checkForEquals(tok);
092: System.out.println("Doc " + tok.sval);
093:
094: if (currentObject.equals("default"))
095: defaultURL = tok.sval;
096: else {
097: Object obj = docPanel.getJ3dFlyContext()
098: .getNameControl().getNamedObject(
099: currentScene, currentObject);
100: if (obj == null)
101: System.out
102: .println("Documentation error, can't find object "
103: + currentObject
104: + " in scene "
105: + currentScene);
106: else
107: objectMap.put(obj, tok.sval);
108: }
109: } else if (tok.sval.equals("scene")) {
110: checkForEquals(tok);
111: currentScene = tok.sval;
112: } else
113: throw new RuntimeException("Unknown token at line "
114: + tok.lineno() + " '" + tok.sval + "'");
115: }
116: } catch (IOException e) {
117: e.printStackTrace();
118: }
119: }
120:
121: /**
122: *Ensure the next token in the stream is an equals sign
123: */
124: private void checkForEquals(StreamTokenizer tok) throws IOException {
125: if (tok.nextToken() != '=')
126: throw new IOException("Syntax Error at line "
127: + tok.lineno() + " " + tok + " expecting =");
128: tok.nextToken();
129: }
130:
131: }
|