1 19 20 package org.netbeans.modules.pdf; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.net.URI ; 28 import java.net.URISyntaxException ; 29 import java.net.URL ; 30 import javax.swing.JMenuItem ; 31 import org.openide.DialogDisplayer; 32 import org.openide.ErrorManager; 33 import org.openide.NotifyDescriptor; 34 import org.openide.awt.Mnemonics; 35 import org.openide.cookies.InstanceCookie; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.loaders.XMLDataObject; 39 import org.openide.modules.InstalledFileLocator; 40 import org.openide.util.NbBundle; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.Node ; 44 import org.w3c.dom.NodeList ; 45 46 47 66 public class LinkProcessor implements InstanceCookie, 67 XMLDataObject.Processor, 68 ActionListener { 69 70 71 public static final String PUBLIC_ID 72 = "-//NetBeans//DTD PDF Document Menu Link 1.0//EN"; 74 public static final String PUBLIC_WWW 75 = "http://www.netbeans.org/dtds/pdf_link-1_0.dtd"; 77 78 private XMLDataObject xmlDataObject; 79 80 81 93 94 95 100 public void attachTo(XMLDataObject xmlDataObject) { 101 this.xmlDataObject = xmlDataObject; 102 } 103 104 105 108 public Class instanceClass() throws IOException , ClassNotFoundException { 109 return JMenuItem .class; 110 } 111 112 113 public Object instanceCreate() throws IOException , ClassNotFoundException { 114 126 127 String name = xmlDataObject.getNodeDelegate().getDisplayName(); 128 129 JMenuItem menuItem = new JMenuItem (); 130 Mnemonics.setLocalizedText(menuItem, name); 131 menuItem.addActionListener(this); 132 133 return menuItem; 134 } 135 136 137 140 public String instanceName() { 141 return xmlDataObject.getName(); 142 } 143 144 149 private String getXMLFileName() { 150 FileObject fileObject = xmlDataObject.getPrimaryFile(); 151 return FileUtil.getFileDisplayName(fileObject); 152 } 153 154 157 private void notifyXMLFileBroken() { 158 String msg = NbBundle.getMessage(LinkProcessor.class, 159 "EXC_file_not_matching_DTD", getXMLFileName()); 161 ErrorManager.getDefault().log(ErrorManager.USER, msg); 162 } 163 164 173 private void notifyBadFileSpec(String msgKey, 174 String urlSpec, 175 boolean isError) { 176 String msg = NbBundle.getMessage(LinkProcessor.class, 177 msgKey, 178 getXMLFileName(), 179 urlSpec); 180 ErrorManager.getDefault().log(isError ? ErrorManager.WARNING 181 : ErrorManager.USER, 182 msg); 183 } 184 185 187 private void notifyFileDoesNotExist(String path) { 188 String msg = NbBundle.getMessage(LinkProcessor.class, 189 "MSG_File_does_not_exist", path); 191 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 192 msg, NotifyDescriptor.WARNING_MESSAGE)); 193 } 194 195 203 private File grabFile(Element innerElement) { 204 String linkType = innerElement.getTagName(); 205 206 207 if (linkType.equals("file")) { if (!innerElement.hasAttribute("path")) { notifyXMLFileBroken(); 210 return null; 211 } 212 return new File (innerElement.getAttribute("path")); 214 215 } else if (linkType.equals("idefile")) { if (!innerElement.hasAttribute("base")) { notifyXMLFileBroken(); 218 return null; 219 } 220 String base = innerElement.getAttribute("base"); String path = base.replace('.', '/') + ".pdf"; File file = InstalledFileLocator.getDefault() 223 .locate(path, null, true); 224 if (file == null) { 225 notifyFileDoesNotExist(path); 226 return null; 227 } 228 return file; 229 230 231 } else if (linkType.equals("url")) { if (!innerElement.hasAttribute("name")) { notifyXMLFileBroken(); 234 return null; 235 } 236 String urlSpec = innerElement.getAttribute("name"); URL url; 238 try { 239 url = new URL (urlSpec); 240 } catch (MalformedURLException ex) { 241 notifyBadFileSpec( 242 "MSG_Cannot_open_malformed_URL", urlSpec, 244 true); 245 return null; 246 } 247 if (!url.getProtocol().equals("file")) { notifyBadFileSpec( 249 "MSG_Cannot_open_unsupported_URL", urlSpec, 251 false); 252 } 253 try { 254 return new File (new URI ("file://" + url.getPath())); } catch (URISyntaxException ex1) { 256 ErrorManager.getDefault().notify(ex1); 257 return null; 258 } catch (IllegalArgumentException ex2) { 259 ErrorManager.getDefault().notify(ex2); 260 return null; 261 } 262 263 } else { 264 notifyXMLFileBroken(); 265 return null; 266 } 267 } 268 269 270 274 public void actionPerformed(ActionEvent evt) { 275 try { 276 277 278 Element innerElement; 279 Document document = xmlDataObject.getDocument(); 280 Element pdfLinkElement = document.getDocumentElement(); 281 NodeList nodeList = pdfLinkElement.getChildNodes(); 282 int count = nodeList.getLength(); 283 Node node = null; 284 for (int i = 0; i < count; i++) { 285 Node nextNode = nodeList.item(i); 286 if (nextNode.getNodeType() == Node.ELEMENT_NODE) { 287 if (node == null) { 288 node = nextNode; 289 } else { 290 291 notifyXMLFileBroken(); 292 return; 293 } 294 } 295 } 296 if (node == null) { 297 298 notifyXMLFileBroken(); 299 return; 300 } 301 innerElement = (Element ) node; 302 303 304 File file = grabFile(innerElement); 305 306 307 if (file != null) { 308 try { 309 new PDFOpenSupport(file).open(); 311 return; 312 } catch (IllegalArgumentException ex) { 313 notifyFileDoesNotExist(file.getPath()); 314 } 315 } 316 } catch (Exception e) { 317 ErrorManager.getDefault().notify(e); 318 } 319 } 320 321 } 322 | Popular Tags |