Revision 8455 trunk/applications/appgvSIG/src/com/iver/cit/gvsig/ProjectExtension.java
ProjectExtension.java | ||
---|---|---|
43 | 43 |
import java.awt.Component; |
44 | 44 |
import java.io.BufferedReader; |
45 | 45 |
import java.io.File; |
46 |
import java.io.FileInputStream; |
|
46 | 47 |
import java.io.FileNotFoundException; |
48 |
import java.io.FileOutputStream; |
|
47 | 49 |
import java.io.FileReader; |
48 | 50 |
import java.io.FileWriter; |
49 | 51 |
import java.io.IOException; |
52 |
import java.io.InputStream; |
|
50 | 53 |
import java.io.InputStreamReader; |
54 |
import java.io.OutputStreamWriter; |
|
51 | 55 |
import java.io.Reader; |
56 |
import java.io.UnsupportedEncodingException; |
|
52 | 57 |
import java.net.MalformedURLException; |
53 | 58 |
import java.net.URL; |
54 | 59 |
import java.text.DateFormat; |
... | ... | |
85 | 90 |
import com.iver.utiles.extensionPoints.ExtensionPoint; |
86 | 91 |
import com.iver.utiles.extensionPoints.ExtensionPoints; |
87 | 92 |
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
93 |
import com.iver.utiles.xml.XMLEncodingUtils; |
|
88 | 94 |
import com.iver.utiles.xmlEntity.generate.XmlTag; |
89 | 95 |
|
90 | 96 |
|
... | ... | |
104 | 110 |
private String lastSavePath; |
105 | 111 |
private String templatesPath; |
106 | 112 |
private WindowInfo seedProjectWindow; |
113 |
/** |
|
114 |
* Use UTF-8 for encoding, as it can represent characters from |
|
115 |
* any language. |
|
116 |
* |
|
117 |
* Another sensible option would be |
|
118 |
* encoding = System.getProperty("file.encoding"); |
|
119 |
* but this would need some extra testing. |
|
120 |
*/ |
|
121 |
private String projectEncoding = "UTF-8"; |
|
107 | 122 |
|
108 | 123 |
/** |
109 | 124 |
* @see com.iver.mdiApp.plugins.IExtension#initialize() |
... | ... | |
403 | 418 |
} |
404 | 419 |
// write it out as XML |
405 | 420 |
try { |
406 |
FileWriter writer = new FileWriter(file.getAbsolutePath()); |
|
421 |
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()); |
|
422 |
OutputStreamWriter writer = new OutputStreamWriter(fos, projectEncoding); |
|
407 | 423 |
Marshaller m = new Marshaller(writer); |
408 |
m.setEncoding("ISO-8859-1");
|
|
424 |
m.setEncoding(projectEncoding);
|
|
409 | 425 |
p.setName(file.getName()); |
410 | 426 |
// p.setPath(file.toString()); |
411 | 427 |
p.setModificationDate(DateFormat.getDateInstance().format(new Date())); |
412 | 428 |
p.setModified(false); |
413 |
m.marshal(p.getXMLEntity().getXmlTag()); |
|
429 |
XMLEntity xml = p.getXMLEntity(); |
|
430 |
xml.putProperty("followHeaderEncoding", true); |
|
431 |
m.marshal(xml.getXmlTag()); |
|
414 | 432 |
PluginServices.getMainFrame().setTitle(file.getName()); |
415 | 433 |
setPath(file.toString()); |
416 | 434 |
|
... | ... | |
421 | 439 |
return true; |
422 | 440 |
} |
423 | 441 |
|
424 |
|
|
425 |
|
|
426 | 442 |
public Project readProject(String path) { |
427 | 443 |
BufferedReader reader =null; |
428 | 444 |
try { |
429 | 445 |
URL url=null; |
430 | 446 |
url = new URL(path); |
431 |
reader = new BufferedReader(new InputStreamReader(url |
|
432 |
.openStream())); |
|
447 |
String encoding = XMLEncodingUtils.getEncoding(url.openStream()); |
|
448 |
InputStream is = url.openStream(); |
|
449 |
if (encoding!=null) { |
|
450 |
Project proj=null; |
|
451 |
try { |
|
452 |
reader = new BufferedReader(new InputStreamReader(is, encoding)); |
|
453 |
proj=readProject(reader, true); |
|
454 |
} catch (UnsupportedEncodingException e) { |
|
455 |
reader = new BufferedReader(new InputStreamReader(is)); |
|
456 |
try { |
|
457 |
Project p=readProject(reader, false); |
|
458 |
} catch (UnsupportedEncodingException e1) { |
|
459 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage())); |
|
460 |
return null; |
|
461 |
} |
|
462 |
} |
|
463 |
ProjectExtension.setPath(path); |
|
464 |
return proj; |
|
465 |
} |
|
466 |
else { |
|
467 |
reader = new BufferedReader(new InputStreamReader(is)); |
|
468 |
Project p; |
|
469 |
try { |
|
470 |
p = readProject(reader, false); |
|
471 |
} catch (UnsupportedEncodingException e) { |
|
472 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e.getLocalizedMessage())); |
|
473 |
return null; |
|
474 |
} |
|
475 |
ProjectExtension.setPath(path); //p.setPath(null); |
|
476 |
return p; |
|
477 |
} |
|
433 | 478 |
} catch (MalformedURLException e) { |
434 | 479 |
File file=new File(path); |
435 | 480 |
return readProject(file); |
436 | 481 |
} catch (IOException e) { |
437 | 482 |
e.printStackTrace(); |
483 |
return null; |
|
438 | 484 |
} |
439 |
Project p=readProject(reader); |
|
440 |
ProjectExtension.setPath(path); //p.setPath(null); |
|
441 |
return p; |
|
442 | 485 |
} |
443 | 486 |
|
487 |
/** |
|
488 |
* Lee del XML el proyecto.<br><br> |
|
489 |
* |
|
490 |
* Reads the XML of the project.<br> |
|
491 |
* It returns a project object holding all needed info that is not linked to the Project Dialog. <br>In case you want the project to be |
|
492 |
* linked to the window you must set this object to the extension:<br> |
|
493 |
* |
|
494 |
* <b>Example:</b><br> |
|
495 |
* |
|
496 |
* ...<br> |
|
497 |
* ...<br> |
|
498 |
* Project p = ProjectExtension.readProject(projectFile);<br> |
|
499 |
* ProjectExtension.setProject(p); |
|
500 |
* ...<br> |
|
501 |
* ...<br> |
|
502 |
* @param file Fichero. |
|
503 |
* |
|
504 |
* @return Project |
|
505 |
* |
|
506 |
*/ |
|
444 | 507 |
public Project readProject(File file) { |
445 | 508 |
File xmlFile = new File(file.getAbsolutePath()); |
446 |
FileReader reader=null; |
|
447 | 509 |
try { |
448 |
reader = new FileReader(xmlFile); |
|
510 |
String encoding = XMLEncodingUtils.getEncoding(new FileInputStream(xmlFile)); |
|
511 |
InputStreamReader reader=null; |
|
512 |
if (encoding!=null) { |
|
513 |
try { |
|
514 |
reader = new InputStreamReader(new FileInputStream(xmlFile), encoding); |
|
515 |
return readProject(reader, true); |
|
516 |
} catch (UnsupportedEncodingException e) { |
|
517 |
reader = new InputStreamReader(new FileInputStream(xmlFile)); |
|
518 |
try { |
|
519 |
return readProject(reader, false); |
|
520 |
} catch (UnsupportedEncodingException e1) { |
|
521 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage())); |
|
522 |
return null; |
|
523 |
} |
|
524 |
} |
|
525 |
} |
|
526 |
else { |
|
527 |
reader = new InputStreamReader(new FileInputStream(xmlFile)); |
|
528 |
try { |
|
529 |
return readProject(reader, false); |
|
530 |
} catch (UnsupportedEncodingException e1) { |
|
531 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage())); |
|
532 |
return null; |
|
533 |
} |
|
534 |
} |
|
449 | 535 |
} catch (FileNotFoundException e) { |
450 | 536 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "fichero_incorrecto")); |
451 | 537 |
return null; |
452 | 538 |
} |
453 |
return readProject(reader);
|
|
454 |
} |
|
539 |
}
|
|
540 |
|
|
455 | 541 |
/** |
456 | 542 |
* Lee del XML el proyecto.<br><br> |
457 | 543 |
* |
458 | 544 |
* Reads the XML of the project.<br> |
459 |
* It returns n project object holding all needed info that is not linked to the Project Dialog. <br>In case you want the project to be
|
|
460 |
* linked to the window you must to set this object to the extension:<br>
|
|
545 |
* It returns a project object holding all needed info that is not linked to the Project Dialog. <br>In case you want the project to be
|
|
546 |
* linked to the window you must set this object to the extension:<br> |
|
461 | 547 |
* |
462 | 548 |
* <b>Example:</b><br> |
463 | 549 |
* |
... | ... | |
467 | 553 |
* ProjectExtension.setProject(p); |
468 | 554 |
* ...<br> |
469 | 555 |
* ...<br> |
470 |
* @param file Fichero. |
|
471 |
* |
|
556 |
* @param reader File reader |
|
557 |
* @param encodingFollowed Whether the encoded specified in the xml header was followed |
|
558 |
* when creating the reader. If the property followHeaderEncoding is false or not set, |
|
559 |
* then the encoding should have not been used when creating the reader, so it must be |
|
560 |
* recreated. |
|
561 |
* |
|
472 | 562 |
* @return Project |
563 |
* @throws UnsupportedEncodingException |
|
473 | 564 |
* |
474 | 565 |
*/ |
475 |
public Project readProject(Reader reader) {
|
|
566 |
private Project readProject(Reader reader, boolean encodingFollowed) throws UnsupportedEncodingException {
|
|
476 | 567 |
Project proj = null; |
477 | 568 |
|
478 | 569 |
try { |
479 |
// File xmlFile = new File(file.getAbsolutePath()); |
|
480 |
// FileReader reader; |
|
481 |
// reader = new FileReader(xmlFile); |
|
482 |
|
|
483 |
|
|
484 | 570 |
XmlTag tag = (XmlTag) XmlTag.unmarshal(reader); |
485 | 571 |
XMLEntity xml=new XMLEntity(tag); |
486 | 572 |
String VERSION=xml.getStringProperty("VERSION"); |
487 |
|
|
573 |
|
|
574 |
if (encodingFollowed) { |
|
575 |
if (xml.contains("followHeaderEncoding")) { |
|
576 |
boolean useEncoding = xml.getBooleanProperty("followHeaderEncoding"); |
|
577 |
if (!useEncoding) { |
|
578 |
throw new UnsupportedEncodingException("the encoding specified in the xml header is not safe"); |
|
579 |
} |
|
580 |
} |
|
581 |
else { |
|
582 |
// Old projects didn't contain followHeaderEncoding and they were |
|
583 |
// not correctly encoded. We throw an exception now, and we'll try |
|
584 |
// to reopen the project |
|
585 |
// using the default system encoding. |
|
586 |
throw new UnsupportedEncodingException("the encoding specified in the xml header is not safe"); |
|
587 |
} |
|
588 |
} |
|
589 |
|
|
488 | 590 |
try { |
489 | 591 |
// if ((VERSION!=null) && (VERSION.equals("0.5") || VERSION.equals("0.4") || (VERSION.indexOf("GISPLANET") != -1))){ |
490 | 592 |
if (VERSION != null) { |
Also available in: Unified diff