31
32 import java.io.File;
33 import java.net.MalformedURLException;
34 import java.net.URL;
35 import java.net.URLClassLoader;
36 import java.util.*;
37
38 /**
39 * Implementation of the HelpSetFactory interface which is aware of javax.help
40 * library.
41 *
42 * @author Dmitry Fazunenko
43 */
44 public class JavaHelpFactory implements HelpSetFactory {
45
46 private static final ResourceBundle i18n = ResourceBundle.getBundle("com.sun.interview.i18n");
47
48 public JavaHelpFactory() {
49 }
50
51 public Object createHelpSetObject(String name, Class c) throws Interview.Fault {
52 ClassLoader cl = c.getClassLoader();
53 String hsn;
54 String pref = "";
55 if (name.startsWith("/")) {
56 hsn = name.substring(1); // strip off leading /
57 pref = hsn.substring(0, hsn.lastIndexOf("/")+1);
58 } else {
59 String cn = c.getName();
60 String pn = cn.substring(0, cn.lastIndexOf('.'));
61 hsn = pn.replace('.', '/') + "/" + name;
62 pref = hsn.substring(0, hsn.indexOf(name));
63 }
64 URL u = HelpSet.findHelpSet(cl, hsn);
65 if (u == null) {
66 throw new HelpNotFoundFault(i18n, "interview.cantFindHelp", hsn);
67 }
68 return new HelpSet(cl, u, pref);
69 }
70
71 public Object createHelpSetObject(String name, File file) throws Interview.Fault {
72 try {
73 URL[] urls = {new URL("file:" + file.getAbsolutePath() + "/")};
74 URLClassLoader cl = new URLClassLoader(urls);
75 URL url = HelpSet.findHelpSet(cl, name);
76 if (url == null) {
77 throw new HelpNotFoundFault(i18n, "interview.cantFindHelp",
78 file.getPath());
79 }
80 HelpSet helpset = new HelpSet(cl, url);
81 return helpset;
82 } catch (MalformedURLException e) {
83 throw new HelpNotFoundFault(i18n, "interview.cantFindHelp", file.getPath());
84 }
85 }
86
87 public Object createHelpID(Object hsObject, String target) {
88 if (hsObject != null) {
89 HelpSet hs = (HelpSet) hsObject;
90 HashMap m = hs.getCombinedMap();
91 if (m != null && !m.isEmpty()) {
92 return HelpID.create(target, hs);
93 }
94 }
95
96 return null;
97 }
98
99 public Object updateHelpSetObject(Interview interview, Object object) {
100 HelpSet newHelpSet = (HelpSet)object;
101 HelpSet oldHelpSet = (HelpSet)interview.getHelpSet();
102 if (interview.getParent() == null) {
103 if (oldHelpSet == null) {
104 // no previously registered helpset
105 // so add in any helpsets for child interviews
106 for (Iterator iter = interview.getInterviews().iterator(); iter.hasNext(); ) {
107 Interview i = (Interview) (iter.next());
108 HelpSet ihs = (HelpSet)i.getHelpSet();
109 if (ihs != null)
110 newHelpSet.add(ihs);
111 }
112 }
113 else {
114 // reregister child help sets with new help set
115 List<HelpSet> helpSetsToRemove = new ArrayList<HelpSet>();
116
117 // transfer help sets old to new
118 for (HelpSet entry : oldHelpSet.getHelpSets()) {
119 helpSetsToRemove.add(entry);
120 newHelpSet.add(entry);
121 } // for
122
123
124 // now remove the sets from the old helpset
125 for (Iterator<HelpSet> iterator = helpSetsToRemove.iterator();
126 iterator.hasNext();) {
127 HelpSet helpToRemove = iterator.next();
|
31
32 import java.io.File;
33 import java.net.MalformedURLException;
34 import java.net.URL;
35 import java.net.URLClassLoader;
36 import java.util.*;
37
38 /**
39 * Implementation of the HelpSetFactory interface which is aware of javax.help
40 * library.
41 *
42 * @author Dmitry Fazunenko
43 */
44 public class JavaHelpFactory implements HelpSetFactory {
45
46 private static final ResourceBundle i18n = ResourceBundle.getBundle("com.sun.interview.i18n");
47
48 public JavaHelpFactory() {
49 }
50
51 public Object createHelpSetObject(String name, Class<?> c) throws Interview.Fault {
52 ClassLoader cl = c.getClassLoader();
53 String hsn;
54 String pref = "";
55 if (name.startsWith("/")) {
56 hsn = name.substring(1); // strip off leading /
57 pref = hsn.substring(0, hsn.lastIndexOf("/")+1);
58 } else {
59 String cn = c.getName();
60 String pn = cn.substring(0, cn.lastIndexOf('.'));
61 hsn = pn.replace('.', '/') + "/" + name;
62 pref = hsn.substring(0, hsn.indexOf(name));
63 }
64 URL u = HelpSet.findHelpSet(cl, hsn);
65 if (u == null) {
66 throw new HelpNotFoundFault(i18n, "interview.cantFindHelp", hsn);
67 }
68 return new HelpSet(cl, u, pref);
69 }
70
71 public Object createHelpSetObject(String name, File file) throws Interview.Fault {
72 try {
73 URL[] urls = {new URL("file:" + file.getAbsolutePath() + "/")};
74 URLClassLoader cl = new URLClassLoader(urls);
75 URL url = HelpSet.findHelpSet(cl, name);
76 if (url == null) {
77 throw new HelpNotFoundFault(i18n, "interview.cantFindHelp",
78 file.getPath());
79 }
80 HelpSet helpset = new HelpSet(cl, url);
81 return helpset;
82 } catch (MalformedURLException e) {
83 throw new HelpNotFoundFault(i18n, "interview.cantFindHelp", file.getPath());
84 }
85 }
86
87 public Object createHelpID(Object hsObject, String target) {
88 if (hsObject != null) {
89 HelpSet hs = (HelpSet) hsObject;
90 HashMap<String, URL> m = hs.getCombinedMap();
91 if (m != null && !m.isEmpty()) {
92 return HelpID.create(target, hs);
93 }
94 }
95
96 return null;
97 }
98
99 public Object updateHelpSetObject(Interview interview, Object object) {
100 HelpSet newHelpSet = (HelpSet)object;
101 HelpSet oldHelpSet = (HelpSet)interview.getHelpSet();
102 if (interview.getParent() == null) {
103 if (oldHelpSet == null) {
104 // no previously registered helpset
105 // so add in any helpsets for child interviews
106 for (Iterator<Interview> iter = interview.getInterviews().iterator(); iter.hasNext(); ) {
107 Interview i = iter.next();
108 HelpSet ihs = (HelpSet)i.getHelpSet();
109 if (ihs != null)
110 newHelpSet.add(ihs);
111 }
112 }
113 else {
114 // reregister child help sets with new help set
115 List<HelpSet> helpSetsToRemove = new ArrayList<HelpSet>();
116
117 // transfer help sets old to new
118 for (HelpSet entry : oldHelpSet.getHelpSets()) {
119 helpSetsToRemove.add(entry);
120 newHelpSet.add(entry);
121 } // for
122
123
124 // now remove the sets from the old helpset
125 for (Iterator<HelpSet> iterator = helpSetsToRemove.iterator();
126 iterator.hasNext();) {
127 HelpSet helpToRemove = iterator.next();
|