PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/SchemaMappingManager.java

#
Java | 706 lines | 500 code | 83 blank | 123 comment | 56 complexity | f36f691c2bfe157878a60fbb193e1fc5 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * SchemaMappingManager.java - select a schema for validation
  3. * :tabSize=4:indentSize=4:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2009, Eric Le Lay
  7. *
  8. * The XML plugin is licensed under the GNU General Public License, with
  9. * the following exception:
  10. *
  11. * "Permission is granted to link this code with software released under
  12. * the Apache license version 1.1, for example used by the Xerces XML
  13. * parser package."
  14. */
  15. package xml;
  16. //{{{ Imports
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.net.URI;
  20. import java.net.MalformedURLException;
  21. import java.net.URISyntaxException;
  22. import javax.swing.JOptionPane;
  23. import javax.swing.JCheckBox;
  24. import javax.swing.JTextField;
  25. import javax.swing.JPanel;
  26. import javax.swing.JLabel;
  27. import javax.swing.JButton;
  28. import javax.swing.BorderFactory;
  29. import java.awt.Color;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.KeyEvent;
  32. import java.awt.event.ActionListener;
  33. import java.util.Map;
  34. import java.util.HashMap;
  35. import java.util.Arrays;
  36. import java.util.regex.Pattern;
  37. import org.gjt.sp.jedit.View;
  38. import org.gjt.sp.jedit.Buffer;
  39. import org.gjt.sp.jedit.jEdit;
  40. import org.gjt.sp.jedit.browser.VFSBrowser;
  41. import org.gjt.sp.jedit.MiscUtilities;
  42. import org.gjt.sp.jedit.GUIUtilities;
  43. import org.gjt.sp.util.Log;
  44. import org.gjt.sp.jedit.gui.EnhancedDialog;
  45. import org.gjt.sp.jedit.gui.HistoryTextField;
  46. import org.xml.sax.ErrorHandler;
  47. import org.xml.sax.SAXException;
  48. import org.xml.sax.SAXParseException;
  49. import common.gui.OkCancelButtons;
  50. import ise.java.awt.KappaLayout;
  51. import xml.parser.SchemaMapping;
  52. import static xml.PathUtilities.*;
  53. //}}}
  54. public final class SchemaMappingManager
  55. {
  56. public static final String SCHEMA_MAPPING_PROP = "xml.schema-mapping";
  57. public static final String BUFFER_ENABLE_SCHEMA_MAPPING_PROP = "xml.enable-schema-mapping";
  58. public static final String ENABLE_SCHEMA_MAPPING_PROP = "buffer."+BUFFER_ENABLE_SCHEMA_MAPPING_PROP;
  59. public static final String BUFFER_SCHEMA_PROP = "xml.validation.schema";
  60. public static final String BUFFER_AUTO_SCHEMA_PROP = "xml.validation.schema-auto";
  61. private static final String BUILT_IN_SCHEMA = "xml/dtds/schemas.xml";
  62. // {{{ singleton constructor
  63. private SchemaMappingManager(){}
  64. // }}}
  65. //{{{ promptSchemaForBuffer() method
  66. /**
  67. * - let the user choose a schema file from the VFSBrowser
  68. */
  69. public static void promptSchemaForBuffer(View view, Buffer buffer)
  70. {
  71. String oldSchema = buffer.getStringProperty(BUFFER_SCHEMA_PROP);
  72. String specificSchema = null;
  73. SchemaMapping lMapping = null;
  74. SchemaMapping gMapping = null;
  75. boolean schemasEnabled = isSchemaMappingEnabled(buffer);
  76. // local schema-mapping
  77. if(schemasEnabled){
  78. specificSchema = MiscUtilities.constructPath(
  79. MiscUtilities.getParentOfPath(
  80. buffer.getPath()),SchemaMapping.SCHEMAS_FILE);
  81. lMapping = getLocalSchemaMapping(buffer, new LoggingErrorHandler(jEdit.getProperty("xml.local-schema")));
  82. gMapping = getGlobalSchemaMapping(new LoggingErrorHandler(jEdit.getProperty("xml.global-schema")));
  83. // always prefer the explicit schema
  84. String oldSchemaAuto = buffer.getStringProperty(BUFFER_AUTO_SCHEMA_PROP);
  85. if(oldSchema == null)oldSchema = oldSchemaAuto;
  86. }
  87. ChooseSchemaDialog choose = new ChooseSchemaDialog(view);
  88. if(choose.choose(buffer.getPath(),urlToPath(oldSchema), true))
  89. {
  90. String bufferURL = pathToURL(buffer.getPath());
  91. URI schemaURL = choose.schemaURL;
  92. if(schemasEnabled)
  93. {
  94. // no schemas.xml in the buffer's directory : will create one
  95. if(lMapping == null)
  96. {
  97. try
  98. {
  99. lMapping = new SchemaMapping(new URI(pathToURL(specificSchema)));
  100. if(gMapping != null)lMapping.ensureIncluded(gMapping);
  101. }
  102. catch(URISyntaxException ue)
  103. {
  104. Log.log(Log.ERROR,SchemaMappingManager.class,ue);
  105. }
  106. }
  107. SchemaMapping.URIResourceRule newRule = new SchemaMapping.URIResourceRule(null,bufferURL,schemaURL.toString(), false);
  108. lMapping.updateMapping(newRule);
  109. try
  110. {
  111. lMapping.toDocument(lMapping.getBaseURI().toString());
  112. }
  113. catch(IOException ioe)
  114. {
  115. // if saving fails, try to output it in a buffer
  116. JOptionPane.showMessageDialog(
  117. view,
  118. jEdit.getProperty("xml-error-to-document.message",new Object[]{ioe.getClass(),ioe.getMessage()}),
  119. jEdit.getProperty("xml-error-to-document.title"),
  120. JOptionPane.ERROR_MESSAGE
  121. );
  122. Buffer b = jEdit.newFile(view,lMapping.getBaseURI().toString());
  123. b.insert(0,lMapping.toString());
  124. view.getEditPane().setBuffer(buffer);
  125. }
  126. }
  127. else
  128. {
  129. buffer.setProperty(BUFFER_SCHEMA_PROP,schemaURL);
  130. }
  131. // finally, use the new schema mapping
  132. view.getInputHandler().setRepeatCount(1);
  133. view.getInputHandler().invokeAction("sidekick-parse");
  134. }
  135. }
  136. //}}}
  137. // {{{ ChooseSchemaDialog class
  138. static class ChooseSchemaDialog extends EnhancedDialog
  139. {
  140. // {{{ private instance variables
  141. private JCheckBox relative_cb;
  142. private JTextField path;
  143. private JTextField buffer_path;
  144. private JTextField relative_path;
  145. private boolean valid;
  146. // }}}
  147. /** URI of the chosen schema */
  148. URI schemaURL = null;
  149. /** is schemaURL relative to the buffer */
  150. boolean relative = false;
  151. /** did the user click OK */
  152. boolean confirmed = false;
  153. ChooseSchemaDialog(final View view)
  154. {
  155. super(view, jEdit.getProperty("xml.choose-schema.title"),true);
  156. JPanel panel = new JPanel( new KappaLayout() );
  157. panel.setBorder( BorderFactory.createEmptyBorder( 6, 6, 6, 6 ) );
  158. ActionListener relativize = new ActionListener(){
  159. public void actionPerformed(ActionEvent e)
  160. {
  161. relativize();
  162. }
  163. };
  164. // reminder : buffer path
  165. JLabel buffer_label = new JLabel( jEdit.getProperty("xml.choose-schema.buffer-path") );
  166. buffer_path = new JTextField(40);
  167. buffer_path.setEditable(false);
  168. buffer_path.setName("buffer_path");
  169. // choose a path
  170. JLabel choose_label = new JLabel( jEdit.getProperty("xml.choose-schema.message") );
  171. path = new HistoryTextField("xml.choose-schema",true);
  172. path.setName("path");
  173. path.setText("");
  174. path.setColumns(40);
  175. path.addActionListener(relativize);
  176. JButton browse = new JButton( jEdit.getProperty("xml.choose-schema.browse") );
  177. browse.setMnemonic(KeyEvent.VK_B);
  178. browse.setName("browse");
  179. browse.addActionListener( new ActionListener()
  180. {
  181. public void actionPerformed( ActionEvent ae )
  182. {
  183. String[] paths = GUIUtilities.showVFSFileDialog( view, path.getText(), VFSBrowser.OPEN_DIALOG, false );
  184. if ( paths != null && paths.length > 0 ) {
  185. path.setText( paths[0] );
  186. relativize();
  187. }
  188. }
  189. });
  190. // set a relative path
  191. relative_cb = new JCheckBox(jEdit.getProperty("xml.choose-schema.relative"));
  192. relative_cb.addActionListener(relativize);
  193. relative_cb.setName("relative");
  194. relative_path = new JTextField(40);
  195. relative_path.setEditable(false);
  196. relative_path.setName("relative_path");
  197. //LTTTTTTT
  198. //S
  199. //LLLLLLLL
  200. //TTTTTTTB
  201. //S
  202. //S
  203. //S
  204. //CCCCCC
  205. //TTTTTTT
  206. //S
  207. // OK CANCEL
  208. JPanel okCancel = new OkCancelButtons(this);
  209. // add the components to the option panel
  210. panel.add( "0, 0, 1, 1, W, w, 3", buffer_label );
  211. panel.add( "1, 0, 6, 1, W, w, 3", buffer_path );
  212. panel.add( "0, 1, 1, 1, 0, , 0", KappaLayout.createVerticalStrut( 11, true ) );
  213. panel.add( "0, 2, 8, 1, W, w, 3", choose_label );
  214. panel.add( "0, 3, 7, 1, W, w, 3", path );
  215. panel.add( "7, 3, 1, 1, W, w, 3", browse );
  216. panel.add( "0, 4, 1, 1, 0, , 0", KappaLayout.createVerticalStrut( 33, true ) );
  217. panel.add( "0, 5, 6, 1, 0, w, 0", relative_cb );
  218. panel.add( "0, 6, 5, 1, W, w, 3", relative_path );
  219. panel.add( "0, 7, 1, 1, 0, , 0", KappaLayout.createVerticalStrut( 11, true ) );
  220. panel.add( "0, 8, 8, 1, 0, w, 0", okCancel );
  221. setContentPane(panel);
  222. pack();
  223. }
  224. /**
  225. * for unit tests, mainly
  226. */
  227. void init(String buffer, String oldSchema, boolean relativeEnabled)
  228. {
  229. buffer_path.setText(buffer);
  230. if(oldSchema == null)
  231. {
  232. path.setText(MiscUtilities.getParentOfPath(buffer));
  233. }
  234. else
  235. {
  236. path.setText(oldSchema);
  237. }
  238. relative_cb.setSelected(false);
  239. relative_cb.setEnabled(relativeEnabled);
  240. relativize();
  241. }
  242. /**
  243. * shows the dialog and returns true if the user confirmed
  244. * @param buffer path to the xml document needing a schema
  245. * @param oldSchema path to the schema currently assigned or null
  246. * @param relativeEnabled is a relative path enabled
  247. */
  248. public boolean choose(String buffer, String oldSchema, boolean relativeEnabled)
  249. {
  250. confirmed = false;
  251. init(buffer,oldSchema, relativeEnabled);
  252. setVisible(true);
  253. return confirmed;
  254. }
  255. /**
  256. * refuse to close the dialog if there is an error
  257. */
  258. public void ok()
  259. {
  260. relativize();
  261. if(valid)
  262. {
  263. confirmed = true;
  264. setVisible(false);
  265. }
  266. }
  267. public void cancel()
  268. {
  269. confirmed = false;
  270. setVisible(false);
  271. }
  272. /**
  273. * transform the contents of path into a relative URL, if relative_cb is checked.
  274. * TODO: handle a relative URL entered by the user
  275. * TODO: relativize jar:file:...!etc URLs
  276. */
  277. private void relativize()
  278. {
  279. relative = relative_cb.isSelected();
  280. try
  281. {
  282. schemaURL = new URI(pathToURL(path.getText()));
  283. if(relative)
  284. {
  285. String bufferURL = pathToURL(MiscUtilities.getParentOfPath(buffer_path.getText()));
  286. schemaURL = new URI(bufferURL).relativize(schemaURL);
  287. }
  288. path.setForeground(Color.BLACK);
  289. valid = true;
  290. relative_path.setText(schemaURL.toString());
  291. }
  292. catch(URISyntaxException ue)
  293. {
  294. Log.log(Log.ERROR,SchemaMappingManager.class,ue);
  295. path.setForeground(Color.RED);
  296. valid = false;
  297. relative_path.setText(ue.getMessage());
  298. }
  299. }
  300. }
  301. // }}}
  302. //{{{ promptTypeIdForBuffer() method
  303. /**
  304. * - gather all type Ids
  305. * - let the user choose one
  306. * - update the SchemaMapping
  307. */
  308. public static void promptTypeIdForBuffer(View view, Buffer buffer)
  309. {
  310. // local schema-mapping
  311. String specificSchema = MiscUtilities.constructPath(
  312. MiscUtilities.getParentOfPath(
  313. buffer.getPath()),SchemaMapping.SCHEMAS_FILE);
  314. URI specificSchemaURI = null;
  315. try
  316. {
  317. specificSchemaURI = new URI(pathToURL(specificSchema));
  318. }
  319. catch(URISyntaxException mfue)
  320. {
  321. // TODO: react to the error
  322. Log.log(Log.ERROR,SchemaMappingManager.class,mfue);
  323. }
  324. SchemaMapping lMapping = getLocalSchemaMapping(buffer, new LoggingErrorHandler(jEdit.getProperty("xml.local-schema")));
  325. SchemaMapping gMapping = getGlobalSchemaMapping(new LoggingErrorHandler(jEdit.getProperty("xml.global-schema")));
  326. SchemaMapping bMapping = getBuiltInSchemaMapping(new LoggingErrorHandler(jEdit.getProperty("xml.builtin-schema")));
  327. Map<String,SchemaMapping> allTypeIds = new HashMap<String,SchemaMapping>();
  328. String oldTypeId = null;
  329. for(SchemaMapping m: Arrays.asList(lMapping, gMapping, bMapping))
  330. {
  331. if(m != null)
  332. {
  333. for(SchemaMapping.TypeIdMapping tid : m.getTypeIds())
  334. {
  335. allTypeIds.put(tid.getId(),m);
  336. }
  337. if(oldTypeId == null)
  338. {
  339. oldTypeId = m.getTypeIdForDocument(buffer.getPath());
  340. }
  341. }
  342. }
  343. String[] tids = allTypeIds.keySet().toArray(new String[]{});
  344. if(tids.length == 0)
  345. {
  346. view.getToolkit().beep();
  347. view.getStatus().setMessage("xml.no-type-id.message");
  348. return;
  349. }
  350. String tid = (String)JOptionPane.showInputDialog(
  351. view,
  352. jEdit.getProperty("xml.choose-type-id.message"),
  353. jEdit.getProperty("xml.choose-type-id.title"),
  354. JOptionPane.OK_CANCEL_OPTION,
  355. null,
  356. tids,
  357. oldTypeId == null ? tids[0] : oldTypeId
  358. );
  359. if(tid!=null)
  360. {
  361. SchemaMapping tidMapping = allTypeIds.get(tid);
  362. if(isSchemaMappingEnabled(buffer))
  363. {
  364. // no schemas.xml in the buffer's directory : will create one
  365. if(lMapping == null)
  366. {
  367. lMapping = new SchemaMapping(specificSchemaURI);
  368. if(gMapping != null)lMapping.ensureIncluded(gMapping);
  369. }
  370. if(lMapping != tidMapping)
  371. {
  372. lMapping.ensureIncluded(tidMapping);
  373. }
  374. String bufferURL = pathToURL(buffer.getPath());
  375. SchemaMapping.URIResourceRule newRule = new SchemaMapping.URIResourceRule(null,bufferURL,tid, true);
  376. lMapping.updateMapping(newRule);
  377. try
  378. {
  379. lMapping.toDocument(lMapping.getBaseURI().toString());
  380. }
  381. catch(IOException ioe)
  382. {
  383. // if saving fails, try to output it in a buffer
  384. JOptionPane.showMessageDialog(
  385. view,
  386. jEdit.getProperty("xml-error-to-document.message",new Object[]{ioe.getClass(),ioe.getMessage()}),
  387. jEdit.getProperty("xml-error-to-document.title"),
  388. JOptionPane.ERROR_MESSAGE
  389. );
  390. Buffer b = jEdit.newFile(view,lMapping.getBaseURI().toString());
  391. b.insert(0,lMapping.toString());
  392. view.getEditPane().setBuffer(buffer);
  393. }
  394. }
  395. else
  396. {
  397. SchemaMapping.Result res = tidMapping.resolveTypeId(tid);
  398. if(res == null)
  399. {
  400. }
  401. else
  402. {
  403. String schemaURL = res.baseURI.resolve(res.target).toString();
  404. buffer.setStringProperty(BUFFER_SCHEMA_PROP,schemaURL);
  405. }
  406. }
  407. // finally, use the new schema mapping
  408. view.getInputHandler().setRepeatCount(1);
  409. view.getInputHandler().invokeAction("sidekick-parse");
  410. }
  411. }
  412. //}}}
  413. //{{{ getGlobalSchemaMapping() method
  414. /**
  415. * @param errorHandler notified of errors loading the global schemas.xml
  416. * @return schema mapping rules in JEDIT_SETTINGS/plugins/xml.XMLPLugin/schemas.xml
  417. */
  418. public static SchemaMapping getGlobalSchemaMapping(ErrorHandler errorHandler)
  419. {
  420. String schemaURL = jEdit.getProperty(SCHEMA_MAPPING_PROP);
  421. SchemaMapping mapping = null;
  422. // if schemaURL is null, it means that there is no settings directory
  423. if(schemaURL != null)
  424. {
  425. Log.log(Log.DEBUG,SchemaMappingManager.class,"global mapping="+schemaURL);
  426. try {
  427. mapping = SchemaMapping.fromDocument(schemaURL,errorHandler);
  428. } catch (IOException e) {
  429. mapping = null;
  430. Log.log(Log.ERROR,SchemaMappingManager.class,
  431. "Error loading global schema mapping: '"+schemaURL+"'",e);
  432. } catch (SAXException e) {
  433. mapping = null;
  434. }
  435. }
  436. return mapping;
  437. }
  438. //}}}
  439. //{{{ getBuiltInSchemaMapping() method
  440. /**
  441. * @param errorHandler notified of errors/warning loading builtin schema mapping (not likely)
  442. * @return mappings shipped with this plugin : jeditresource:XML.jar!/xml/dtds/schemas.xml
  443. */
  444. public static SchemaMapping getBuiltInSchemaMapping(ErrorHandler errorHandler)
  445. {
  446. java.net.URL schemaURL = SchemaMappingManager.class.getClassLoader().getResource(BUILT_IN_SCHEMA);
  447. if(schemaURL == null)
  448. {
  449. throw new AssertionError("built-in schemas.xml cant be found !");
  450. }
  451. else
  452. {
  453. try {
  454. return SchemaMapping.fromDocument(schemaURL.toString(),errorHandler);
  455. } catch (IOException e) {
  456. Log.log(Log.ERROR,SchemaMappingManager.class,
  457. "Error loading local schema mapping : '"+schemaURL+"'",e);
  458. return null;
  459. } catch (SAXException e) {
  460. return null;
  461. }
  462. }
  463. }
  464. //}}}
  465. //{{{ getLocalSchemaMapping() method
  466. /**
  467. * @return schemas.xml next to buffer or null if it doesn't exist
  468. */
  469. public static SchemaMapping getLocalSchemaMapping(Buffer buffer, ErrorHandler errorHandler)
  470. {
  471. SchemaMapping mapping;
  472. // local schema-mapping
  473. String specificSchema = MiscUtilities.constructPath(
  474. MiscUtilities.getParentOfPath(
  475. buffer.getPath()),SchemaMapping.SCHEMAS_FILE);
  476. // VFS-compatible
  477. try
  478. {
  479. URI uriSpecificSchema = new URI(PathUtilities.pathToURL(specificSchema));
  480. if(SchemaMapping.resourceExists(uriSpecificSchema))
  481. {
  482. String schemaURL = uriSpecificSchema.toString();
  483. mapping = SchemaMapping.fromDocument(schemaURL, errorHandler);
  484. }
  485. else
  486. {
  487. Log.log(Log.DEBUG, SchemaMappingManager.class,
  488. "no schemas.xml in "+specificSchema);
  489. mapping = null;
  490. }
  491. }
  492. catch(URISyntaxException ue)
  493. {
  494. // may happen if specificSchema is relative, but it should not be the case with buffer.getPath()
  495. Log.log(Log.ERROR,SchemaMappingManager.class,
  496. "Error converting path for fromDocument : '"+specificSchema+"'",ue);
  497. mapping = null;
  498. } catch (IOException e) {
  499. mapping = null;
  500. Log.log(Log.ERROR,SchemaMappingManager.class,
  501. "Error loading local schema mapping : '"+specificSchema+"'",e);
  502. } catch (SAXException e) {
  503. // logging has already been taken care of
  504. mapping = null;
  505. }
  506. return mapping;
  507. }
  508. //}}}
  509. //{{{ getSchemaMappingForBuffer() method
  510. /**
  511. * @return local SchemaMapping or global SchemaMapping or built-in SchemaMapping
  512. */
  513. public static SchemaMapping getSchemaMappingForBuffer(Buffer buffer, ErrorHandler errorHandler)
  514. {
  515. SchemaMapping mapping;
  516. // local schema-mapping ; be sure not to continue on validation error by encapsulating the errorHandler in a LoggingErrorHandler
  517. mapping = getLocalSchemaMapping(buffer, new LoggingErrorHandler(jEdit.getProperty(jEdit.getProperty("xml.local-schema")), errorHandler));
  518. if(mapping == null)
  519. {
  520. mapping = getGlobalSchemaMapping(new LoggingErrorHandler(jEdit.getProperty(jEdit.getProperty("xml.global-schema")), errorHandler));
  521. }
  522. if(mapping == null)
  523. {
  524. Log.log(Log.DEBUG, SchemaMappingManager.class,
  525. "no settings => using built-in schema mapping file");
  526. mapping = getBuiltInSchemaMapping(new LoggingErrorHandler(jEdit.getProperty(jEdit.getProperty("xml.builtin-schema")), errorHandler));
  527. }
  528. return mapping;
  529. }
  530. //}}}
  531. //{{{ initGlobalSchemaMapping() method
  532. /**
  533. * Finds (and creates if needed) the global schema mapping (schemas.xml)
  534. * file in the settings directory, for reading or overwriting it.
  535. */
  536. public static void initGlobalSchemaMapping(View view){
  537. jEdit.unsetProperty(SCHEMA_MAPPING_PROP);
  538. File home = org.gjt.sp.jedit.EditPlugin.getPluginHome(xml.XmlPlugin.class);
  539. if(home == null)
  540. {
  541. // -nosettings
  542. return;
  543. }
  544. else if(!home.exists())
  545. {
  546. Log.log(Log.DEBUG,SchemaMappingManager.class, "creating settings directory");
  547. try
  548. {
  549. boolean created = home.mkdirs();
  550. if(!created)
  551. {
  552. GUIUtilities.error( view, "unable to create settings directory: "+home,null);
  553. return;
  554. }
  555. }
  556. catch(SecurityException se)
  557. {
  558. GUIUtilities.error( view, "unable to create settings directory (security exception): "+home,null);
  559. return;
  560. }
  561. }
  562. File schemas = new File(home,SchemaMapping.SCHEMAS_FILE);
  563. // create an empty mapping file in settings directory
  564. // it points to the global schemaMapping, to get all
  565. // the builtin rules
  566. if(!schemas.exists()){
  567. SchemaMapping builtinMapping = getBuiltInSchemaMapping(new LoggingErrorHandler(jEdit.getProperty("xml.builtin-schema")));
  568. SchemaMapping tmp = new SchemaMapping();
  569. tmp.ensureIncluded(builtinMapping);
  570. try{
  571. tmp.toDocument(schemas.toURI().toURL().toString());
  572. }catch(IOException ioe){
  573. Log.log(Log.ERROR,SchemaMappingManager.class,"Unable to save default RelaxNG mappings",ioe);
  574. return;
  575. }
  576. }
  577. try
  578. {
  579. jEdit.setProperty(SCHEMA_MAPPING_PROP,schemas.toURI().toURL().toString());
  580. }
  581. catch(java.net.MalformedURLException mfe)
  582. {
  583. Log.log(Log.ERROR,SchemaMappingManager.class,mfe);
  584. }
  585. }
  586. //}}}
  587. //{{{ isSchemaMappingEnabled() method
  588. public static boolean isSchemaMappingEnabled(Buffer b){
  589. return b.getBooleanProperty(BUFFER_ENABLE_SCHEMA_MAPPING_PROP);
  590. }
  591. //}}}
  592. //{{{ LoggingErrorHandler class
  593. /** logs errors and warnings to Activity Log and rethrow exception on error and fatalError to prevent
  594. * invalid documents to result in invalid SchemaMappings.
  595. */
  596. public static class LoggingErrorHandler implements ErrorHandler{
  597. private String resourceName;
  598. private ErrorHandler errorHandler;
  599. public LoggingErrorHandler(String resourceName){
  600. }
  601. public LoggingErrorHandler(String property, ErrorHandler errorHandler) {
  602. this.resourceName = resourceName;
  603. this.errorHandler = errorHandler;
  604. }
  605. @Override
  606. public void error(SAXParseException exception) throws SAXException {
  607. Log.log(Log.WARNING, SchemaMappingManager.class, jEdit.getProperty("xml.SchemaMappingManager.error",new Object[]{resourceName,exception.getSystemId(),exception.getLineNumber(),exception.getLocalizedMessage()}));
  608. if(errorHandler != null)errorHandler.error(exception);
  609. throw exception;
  610. }
  611. @Override
  612. public void fatalError(SAXParseException exception) throws SAXException {
  613. Log.log(Log.WARNING, SchemaMappingManager.class, jEdit.getProperty("xml.SchemaMappingManager.fatal-error",new Object[]{resourceName,exception.getSystemId(),exception.getLineNumber(),exception.getLocalizedMessage()}));
  614. if(errorHandler != null)errorHandler.fatalError(exception);
  615. throw exception;
  616. }
  617. @Override
  618. public void warning(SAXParseException exception) throws SAXException {
  619. Log.log(Log.NOTICE, SchemaMappingManager.class, jEdit.getProperty("xml.SchemaMappingManager.warning",new Object[]{resourceName,exception.getSystemId(),exception.getLineNumber(),exception.getLocalizedMessage()}));
  620. if(errorHandler != null)errorHandler.warning(exception);
  621. }
  622. }
  623. }