PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/java_plugins/editorPlugin/com/livinge/eplugin/editor/WeSettings.java

https://github.com/drietsch/newcms
Java | 755 lines | 463 code | 198 blank | 94 comment | 67 complexity | 8622e36553cef40181a02e3d66a1b9ac MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. /**
  2. * webEdition CMS
  3. *
  4. * This source is part of webEdition CMS. webEdition CMS is
  5. * free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * any later version.
  9. *
  10. * The GNU General Public License can be found at
  11. * http://www.gnu.org/copyleft/gpl.html.
  12. * A copy is found in the textfile license.txt
  13. *
  14. * @copyright Copyright (c) 2008 living-e AG (http://www.living-e.com)
  15. * @license http://www.gnu.org/copyleft/gpl.html GPL
  16. */
  17. package com.livinge.eplugin.editor;
  18. import java.io.BufferedReader;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.io.OutputStreamWriter;
  25. import java.net.URL;
  26. import java.nio.charset.Charset;
  27. import java.security.AccessController;
  28. import java.util.Enumeration;
  29. import java.util.Hashtable;
  30. import java.util.Iterator;
  31. import java.util.Properties;
  32. import java.util.Vector;
  33. import javax.xml.parsers.DocumentBuilder;
  34. import javax.xml.parsers.DocumentBuilderFactory;
  35. import javax.xml.parsers.ParserConfigurationException;
  36. import org.w3c.dom.Document;
  37. import org.w3c.dom.Node;
  38. import org.w3c.dom.NodeList;
  39. import org.xml.sax.SAXException;
  40. import com.livinge.eplugin.privileged.PrivilegedSave;
  41. import com.livinge.eplugin.registry.MacRegistry;
  42. import com.livinge.eplugin.registry.WinRegistry;
  43. public class WeSettings{
  44. private String buildVersion="5.1_9";
  45. private String defRegistryCompanyDir="living-e";
  46. private String defRegistryProductDir="editorPlugin";
  47. private String defEditorFile="editor.xml";
  48. private String defSettingsFile="setting.xml";
  49. private String defCacheDir="cache";
  50. public String registryDir;
  51. private String editorFile;
  52. private String settingsFile;
  53. public String cacheDir;
  54. public String lastContentType = "";
  55. private Hashtable ParamList=new Hashtable();
  56. private Hashtable Settings=new Hashtable();
  57. //private Vector EditorList=new Vector();
  58. private Vector DefaultEditorList=new Vector();
  59. private Vector SystemEditorList=new Vector();
  60. private Vector ContentTypes = new Vector();
  61. private Vector ContentTypeIcons = new Vector();
  62. private URL HostUrl;
  63. private Properties PluginProperties;
  64. public String AppDir = new String("webEdition");
  65. public WeSettings(){
  66. /*System.out.println("file.separator = " + System.getProperty("file.separator"));
  67. System.out.println("line.separator (windows & unix platform) = \\n");
  68. System.out.println("line.separator (actual) = " + System.getProperty("line.separator"));
  69. System.out.println("path.separator = " + System.getProperty("path.separator"));
  70. System.out.println("java.class.version = " + System.getProperty("java.class.version"));
  71. System.out.println("java.vendor = " + System.getProperty("java.vendor"));
  72. System.out.println("java.vendor.url = " + System.getProperty("java.vendor.url"));
  73. System.out.println("java.version = " + System.getProperty("java.version"));
  74. System.out.println("os.arch = " + System.getProperty("os.arch"));
  75. System.out.println("os.name = " + System.getProperty("os.name"));
  76. System.out.println("os.version = " + System.getProperty("os.version"));
  77. System.out.println("Properties: (Applications only or Trusted Applets)");
  78. System.out.println("java.class.path = " + System.getProperty("java.class.path"));
  79. System.out.println("java.home = |" + System.getProperty("java.home"));
  80. System.out.println("user.dir = " + System.getProperty("user.dir"));
  81. System.out.println("user.home = " + System.getProperty("user.home"));
  82. System.out.println("user.name = " + System.getProperty("user.name"));*/
  83. //super();
  84. //System.out.println("os.name = " + System.getProperty("os.name"));
  85. // initialize content types
  86. ContentTypes.add("text/plain");
  87. ContentTypes.add("text/js");
  88. ContentTypes.add("text/css");
  89. ContentTypes.add("text/html");
  90. ContentTypes.add("text/xml");
  91. ContentTypes.add("text/webedition");
  92. ContentTypes.add("text/weTmpl");
  93. ContentTypes.add("image/*");
  94. ContentTypes.add("video/quicktime");
  95. ContentTypes.add("application/x-shockwave-flash");
  96. ContentTypes.add("application/*");
  97. ContentTypeIcons.add("link.gif"); //text/plain
  98. ContentTypeIcons.add("prog.gif"); //text/js
  99. ContentTypeIcons.add("prog.gif"); //text/css
  100. ContentTypeIcons.add("prog.gif"); //text/html
  101. ContentTypeIcons.add("link.gif"); //text/xml
  102. ContentTypeIcons.add("we_dokument.gif"); //text/webedition
  103. ContentTypeIcons.add("prog.gif"); //text/weTmpl
  104. ContentTypeIcons.add("image.gif"); //image/*
  105. ContentTypeIcons.add("film.gif"); //video/quicktime
  106. ContentTypeIcons.add("film.gif"); //application/x-shockwave-flash
  107. ContentTypeIcons.add("link.gif"); //application/*
  108. Settings.put("askForEditor", "true");
  109. PluginProperties = new Properties();
  110. try {
  111. InputStream is = getClass().getResourceAsStream("eplugin.properties");
  112. PluginProperties.load(is);
  113. } catch (IOException e) {
  114. } catch (NullPointerException e) {
  115. }
  116. String OS = System.getProperty("os.name").toLowerCase();
  117. String appDataPath = "";
  118. String sep = System.getProperty("file.separator");
  119. if(OS.matches("mac os x")) {
  120. appDataPath = System.getProperty("user.home")+ sep + "Library" + sep + "Application Support" + sep;
  121. } else if (OS.matches("windows(.)*")) {
  122. Process p = null;
  123. try {
  124. if (OS.indexOf("windows 9") > -1) {
  125. p = Runtime.getRuntime().exec("command.com /c echo %APPDATA%");
  126. } else {
  127. p = Runtime.getRuntime().exec("cmd.exe /c echo %APPDATA%");
  128. }
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132. BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
  133. try {
  134. appDataPath = br.readLine();
  135. } catch (IOException e) {
  136. e.printStackTrace();
  137. }
  138. if (appDataPath.length() == 0) {
  139. appDataPath=System.getProperty("user.home") + sep + "Application Data" + sep;
  140. } else {
  141. appDataPath = appDataPath + sep;
  142. }
  143. System.out.println(appDataPath);
  144. } else {
  145. System.out.println("Only Mac OSX and Windows are supported by the editor plugin!");
  146. }
  147. registryDir=appDataPath+defRegistryCompanyDir+sep+defRegistryProductDir+sep;
  148. editorFile = registryDir+System.getProperty("file.separator")+defEditorFile;
  149. settingsFile = registryDir+System.getProperty("file.separator")+defSettingsFile;
  150. loadSettings();
  151. }
  152. public void createCache() {
  153. String host = HostUrl.getHost();
  154. cacheDir=registryDir+defCacheDir+System.getProperty("file.separator")+host+System.getProperty("file.separator");
  155. createDir(registryDir);
  156. createDir(cacheDir);
  157. }
  158. private boolean createDir(String dir){
  159. boolean exists = (new File(dir)).exists();
  160. if (!exists) return (new File(dir)).mkdirs();
  161. return false;
  162. }
  163. public void addEditor(WeEditor Editor,Vector List){
  164. boolean found = false;
  165. for(Iterator i = List.iterator();i.hasNext();) {
  166. WeEditor we = (WeEditor)i.next();
  167. if(Editor.Path.equals(we.Path)){
  168. found = true;
  169. }
  170. }
  171. if(!found) {
  172. List.add(Editor);
  173. }
  174. }
  175. public void addToDefaultEditors(WeEditor editor) {
  176. addEditor(editor,DefaultEditorList);
  177. }
  178. public void setParam(String name,String value){
  179. try{
  180. ParamList.put(name,value);
  181. }catch(NullPointerException e) {
  182. System.out.print("Missing param: " + name + "--" + value + "\n");
  183. }
  184. }
  185. public boolean isParam(String name){
  186. String val = (String)ParamList.get(name);
  187. return (val != null);
  188. }
  189. public String getParam(String name){
  190. return (String) ParamList.get(name);
  191. }
  192. /*public void saveSettings2(){
  193. registryFile = registryDir+System.getProperty("file.separator")+defRegistryFile;
  194. WeEditor editor;
  195. try{
  196. java.beans.XMLEncoder xmlEnc = new java.beans.XMLEncoder(
  197. new BufferedOutputStream(new FileOutputStream(registryFile)));
  198. for(int k=0;k<EditorList.size();k++){
  199. editor=(WeEditor) EditorList.elementAt(k);
  200. xmlEnc.writeObject(editor);
  201. }
  202. xmlEnc.close();
  203. } catch (IOException e) {
  204. System.out.println(e);
  205. }
  206. }
  207. public void loadSettings2(){
  208. registryFile = registryDir+System.getProperty("file.separator")+defRegistryFile;
  209. try{
  210. java.beans.XMLDecoder d = new java.beans.XMLDecoder(new BufferedInputStream(new FileInputStream(registryFile)));
  211. EditorList= (Vector) d.readObject();
  212. d.close();
  213. } catch (IOException e) {
  214. System.out.println(e);
  215. }
  216. } */
  217. public void saveDefaultEditorList(){
  218. WeEditor editor;
  219. String out="";
  220. out="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+ System.getProperty("line.separator");
  221. out+="<webEdition>"+ System.getProperty("line.separator");
  222. for(int i=0;i<DefaultEditorList.size();i++){
  223. editor = (WeEditor) DefaultEditorList.elementAt(i);
  224. out+=editor.object2xml();
  225. }
  226. out+="</webEdition>"+ System.getProperty("line.separator");
  227. PrivilegedSave pSave = new PrivilegedSave(editorFile,out,"UTF-8");
  228. AccessController.doPrivileged(pSave);
  229. }
  230. public void loadEditorList(String filename,String ct) {
  231. loadDefaultEditorList();
  232. loadSystemEditorList(filename,ct);
  233. }
  234. protected void loadSystemEditorList(String filename,String ct) {
  235. SystemEditorList.removeAllElements();
  236. int index = filename.lastIndexOf('.');
  237. if(index>-1) {
  238. String ext = filename.substring(index+1);
  239. if((System.getProperty("os.name").matches("Mac OS X")) ||
  240. (System.getProperty("os.name").matches("Windows(.)*"))
  241. ) {
  242. Vector appList = new Vector();
  243. if(System.getProperty("os.name").matches("Mac OS X")) {
  244. MacRegistry mr = new MacRegistry(this);
  245. appList = mr.getAppList(ext);
  246. }
  247. if(System.getProperty("os.name").matches("Windows(.)*")) {
  248. WinRegistry wr = new WinRegistry();
  249. appList = wr.getAppList(ext);
  250. }
  251. if(appList.size()>0) {
  252. WeEditor ed=new WeEditor();
  253. String path;
  254. String name;
  255. for(Iterator i = appList.iterator();i.hasNext();) {
  256. path = i.next().toString();
  257. name = ((new File(path)).getName()).replaceAll(".app", "");
  258. ed=new WeEditor();
  259. ed.init(name,path,"",ct,"","");
  260. addEditor(ed,SystemEditorList);
  261. }
  262. }
  263. } else {
  264. }
  265. }
  266. }
  267. protected void loadDefaultEditorList(){
  268. DefaultEditorList.removeAllElements();
  269. if(!(new File(editorFile)).exists()) return;
  270. Document document;
  271. DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
  272. //factory.setValidating(true);
  273. //factory.setNamespaceAware(true);
  274. try {
  275. DocumentBuilder builder = factory.newDocumentBuilder();
  276. document = builder.parse( new File(editorFile) );
  277. // get editors
  278. NodeList childs=document.getFirstChild().getChildNodes();
  279. int childCount=childs.getLength();
  280. Node node;
  281. //NamedNodeMap map;
  282. DefaultEditorList=new Vector();
  283. for(int i=0;i<childCount;i++){
  284. node=childs.item(i);
  285. if(node.getNodeName()=="Editor"){
  286. NodeList editorProps=node.getChildNodes();
  287. String Name=new String();
  288. String Path=new String();
  289. String Args=new String();
  290. String ContentType=new String();
  291. String DefaultFor=new String();
  292. String Encoding=new String();
  293. for(int j=0;j<editorProps.getLength();j++){
  294. try {
  295. if(editorProps.item(j).getNodeName()=="Name") Name=editorProps.item(j).getFirstChild().getNodeValue();
  296. }catch(NullPointerException e) {
  297. //System.err.println("No name:" + e);
  298. }
  299. try {
  300. if(editorProps.item(j).getNodeName()=="Path") Path=editorProps.item(j).getFirstChild().getNodeValue();
  301. }catch(NullPointerException e) {
  302. //System.err.println("No path:" + e);
  303. }
  304. try {
  305. if(editorProps.item(j).getNodeName()=="Args") Args=editorProps.item(j).getFirstChild().getNodeValue();
  306. }catch(NullPointerException e) {
  307. //System.err.println("No path:" + e);
  308. }
  309. try {
  310. if(editorProps.item(j).getNodeName()=="ContentType") ContentType=editorProps.item(j).getFirstChild().getNodeValue();
  311. }catch(NullPointerException e) {
  312. //System.err.println("No content type:" + e);
  313. }
  314. try {
  315. if(editorProps.item(j).getNodeName()=="DefaultFor") DefaultFor=editorProps.item(j).getFirstChild().getNodeValue();
  316. }catch(NullPointerException e) {
  317. //System.err.println("No default for: " + e);
  318. }
  319. try {
  320. if(editorProps.item(j).getNodeName()=="Encoding") Encoding=editorProps.item(j).getFirstChild().getNodeValue();
  321. }catch(NullPointerException e) {
  322. //System.err.println("No Encoding:" + e);
  323. }
  324. }
  325. if(!Path.equals("")){
  326. WeEditor ed=new WeEditor();
  327. ed.init(Name,Path,Args,ContentType,DefaultFor,Encoding);
  328. addEditor(ed,DefaultEditorList);
  329. }
  330. }
  331. }
  332. } catch (SAXException sxe) {
  333. } catch (ParserConfigurationException pce) {
  334. } catch (IOException ioe) {
  335. } catch (Exception e) {
  336. }
  337. }
  338. public boolean hasContentType(String list,String ct) {
  339. if((ct.trim()).equals("")) {
  340. return true;
  341. }
  342. if((list.trim()).equals("")) {
  343. return false;
  344. }
  345. String arr[] = list.split(",");
  346. String it;
  347. for(int i = 0;i<arr.length;i++) {
  348. it = (arr[i].trim());
  349. if(it.length()>0 && it.equals(ct)) {
  350. return true;
  351. }
  352. }
  353. return false;
  354. }
  355. /*public void resetEditorList() {
  356. EditorList.removeAllElements();
  357. EditorList = new Vector();
  358. }*/
  359. public void clearList(){
  360. DefaultEditorList.removeAllElements();
  361. DefaultEditorList = new Vector();
  362. saveDefaultEditorList();
  363. }
  364. public Vector getEditorList(String ct){
  365. Vector edList = new Vector();
  366. for(Iterator i = DefaultEditorList.iterator();i.hasNext();) {
  367. WeEditor ed= (WeEditor)i.next();
  368. if(!ed.Path.equals("") && hasContentType(ed.ContentType,ct)){
  369. addEditor(ed,edList);
  370. }
  371. }
  372. for(Iterator i = SystemEditorList.iterator();i.hasNext();) {
  373. WeEditor ed= (WeEditor)i.next();
  374. if(!ed.Path.equals("") && hasContentType(ed.ContentType,ct)){
  375. addEditor(ed,edList);
  376. }
  377. }
  378. return edList;
  379. }
  380. public boolean editorExists(String path){
  381. Vector edList = getEditorList("");
  382. for(int i=0;i<edList.size();i++){
  383. if(((WeEditor)edList.elementAt(i)).Path.equals(path)) return true;
  384. }
  385. return false;
  386. }
  387. public WeEditor getDefaultEditor(String ct){
  388. for(Iterator i = DefaultEditorList.iterator();i.hasNext();) {
  389. WeEditor ed = (WeEditor)i.next();
  390. String edarr[] = ed.DefaultFor.split(",");
  391. String it;
  392. for(int j = 0;j<edarr.length;j++) {
  393. it = (edarr[j].trim());
  394. if(it.length()>0 && it.equals(ct)) {
  395. return ed;
  396. }
  397. }
  398. }
  399. return null;
  400. }
  401. public WeEditor getEditorAt(int index){
  402. Vector edList = getEditorList("");
  403. return (WeEditor) edList.elementAt(index);
  404. }
  405. public Vector getContentTypes(){
  406. return (Vector) ContentTypes.clone();
  407. }
  408. public void setUrl(String url) {
  409. try {
  410. HostUrl = new URL(url);
  411. } catch (Exception e) {
  412. // do nothing
  413. }
  414. }
  415. public URL getUrl(){
  416. return HostUrl;
  417. }
  418. public void setAppDir(String app) {
  419. AppDir = app;
  420. }
  421. public String getAppDir() {
  422. return AppDir;
  423. }
  424. public String getHost() {
  425. return HostUrl.toExternalForm();
  426. }
  427. public void removeFromDefaultEditorList(WeEditor editor) {
  428. DefaultEditorList.removeElement(editor);
  429. saveDefaultEditorList();
  430. }
  431. public boolean isInDefaultEditorList(WeEditor editor) {
  432. for(Iterator i = DefaultEditorList.iterator();i.hasNext();) {
  433. WeEditor ed = (WeEditor)i.next();
  434. if(editor.Path.equals(ed.Path)) {
  435. return true;
  436. }
  437. }
  438. return false;
  439. }
  440. public void replaceEditor(WeEditor editor) {
  441. int i = 0;
  442. int size = DefaultEditorList.size();
  443. for(i = 0;i<size;i++) {
  444. WeEditor ed = (WeEditor)DefaultEditorList.elementAt(i);
  445. if(ed.Path.equals(editor.Path)) {
  446. break;
  447. }
  448. }
  449. if(i<size) {
  450. DefaultEditorList.setElementAt(editor, i);
  451. }
  452. }
  453. public void saveSettings() {
  454. String out="";
  455. out="<?xml version=\"1.0\"?>"+ System.getProperty("line.separator");
  456. out+="<webEdition>"+ System.getProperty("line.separator");
  457. Enumeration e = Settings.keys();
  458. for(;e.hasMoreElements();) {
  459. out+="\t<Setting>" + System.getProperty("line.separator");
  460. String key = (String)e.nextElement();
  461. out+="\t\t<Name>" + key + "</Name>" + System.getProperty("line.separator");
  462. out+="\t\t<Value>" + Settings.get(key) + "</Value>" + System.getProperty("line.separator");
  463. out+="\t</Setting>" + System.getProperty("line.separator");
  464. }
  465. out+="</webEdition>"+ System.getProperty("line.separator");
  466. PrivilegedSave pSave = new PrivilegedSave(settingsFile,out);
  467. AccessController.doPrivileged(pSave);
  468. }
  469. public void loadSettings() {
  470. Document document;
  471. DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
  472. try {
  473. File sFile = new File(settingsFile);
  474. if(!sFile.exists()) {
  475. sFile.getParentFile().mkdirs();
  476. sFile.createNewFile();
  477. } else {
  478. if(sFile.length()>0) {
  479. DocumentBuilder builder = factory.newDocumentBuilder();
  480. document = builder.parse( sFile );
  481. // get editors
  482. NodeList childs=document.getFirstChild().getChildNodes();
  483. int childCount=childs.getLength();
  484. Node node;
  485. //NamedNodeMap map;
  486. for(int i=0;i<childCount;i++){
  487. node=childs.item(i);
  488. if(node.getNodeName()=="Setting"){
  489. NodeList settingProps=node.getChildNodes();
  490. String Name=new String();
  491. String Value=new String();
  492. for(int j=0;j<settingProps.getLength();j++){
  493. try {
  494. if(settingProps.item(j).getNodeName()=="Name") Name=settingProps.item(j).getFirstChild().getNodeValue();
  495. }catch(NullPointerException e) {
  496. //System.err.println("No name:" + e);
  497. }
  498. try {
  499. if(settingProps.item(j).getNodeName()=="Value") Value=settingProps.item(j).getFirstChild().getNodeValue();
  500. }catch(NullPointerException e) {
  501. //System.err.println("No value:" + e);
  502. }
  503. }
  504. if(!Name.equals("")){
  505. Settings.put(Name, Value);
  506. }
  507. }
  508. }
  509. }
  510. }
  511. } catch (SAXException sxe) {
  512. } catch (ParserConfigurationException pce) {
  513. } catch (IOException ioe) {
  514. } catch (Exception e) {
  515. }
  516. }
  517. public void setSetting(String name,String value){
  518. try{
  519. Settings.put(name,value);
  520. }catch(NullPointerException e) {
  521. System.out.print("Missing setting: " + name + "--" + value + "\n");
  522. }
  523. }
  524. public String getSetting(String name){
  525. return (String) Settings.get(name);
  526. }
  527. public static String getDefaultEncoding() {
  528. /*PrivilegedSystemProperties p = new PrivilegedSystemProperties("file.encoding");
  529. AccessController.doPrivileged(p);
  530. return p.Value.replaceFirst("Cp", "windows-");*/
  531. return new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding().replaceFirst("Cp", "windows-");
  532. }
  533. public static boolean isCharsetSupported(String charset) {
  534. try{
  535. if(Charset.isSupported(charset)) {
  536. return true;
  537. } else {
  538. return false;
  539. }
  540. } catch (Exception e){
  541. return false;
  542. }
  543. }
  544. public String getVersion() {
  545. return buildVersion;
  546. }
  547. }