001: package com.xoetrope.carousel.services;
002:
003: import java.io.BufferedInputStream;
004: import java.io.BufferedReader;
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.FileNotFoundException;
008: import java.io.FileOutputStream;
009: import java.io.FileReader;
010: import java.io.FileWriter;
011: import java.io.IOException;
012: import java.io.InputStream;
013: import java.io.InputStreamReader;
014: import java.net.URL;
015: import java.util.Enumeration;
016: import java.util.Properties;
017: import java.util.Vector;
018: import net.n3.nanoxml.IXMLElement;
019: import net.xoetrope.data.XDataSource;
020: import net.xoetrope.debug.DebugLogger;
021: import net.xoetrope.optional.data.XOptionalDataSource;
022: import net.xoetrope.optional.service.XRouteManager;
023: import net.xoetrope.xml.XmlElement;
024: import net.xoetrope.xml.XmlSource;
025: import net.xoetrope.xml.nanoxml.NanoXmlElement;
026: import net.xoetrope.xui.data.XModel;
027: import net.xoetrope.xui.XProject;
028: import net.xoetrope.editor.project.XEditorProject;
029: import net.xoetrope.editor.project.XEditorProjectManager;
030: import net.xoetrope.editor.project.XEditorProjectProperty;
031: import net.xoetrope.xui.data.XModelHelper;
032: import net.xoetrope.xml.XmlWriter;
033: import net.xoetrope.xml.nanoxml.NanoXmlParser;
034: import net.xoetrope.xml.nanoxml.NanoXmlWriter;
035:
036: import net.xoetrope.xui.data.XBaseModel;
037: import net.xoetrope.xui.helper.Constants;
038:
039: /**
040: * Manager for keeping track of loaded routes and routes being edited.
041: * Saves the route information when the save menu is clicked in the editor.
042: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
043: * the GNU Public License (GPL), please see license.txt for more details. If
044: * you make commercial use of this software you must purchase a commercial
045: * license from Xoetrope.</p>
046: * <p> $Revision: 1.15 $</p>
047: */
048: public class RouteManager extends XRouteManager {
049: private XBaseModel routeDefsMdl;
050: private XBaseModel customRouteDefsMdl;
051: private XBaseModel clientRoutesMdl, serverRoutesMdl;
052: private ModelManager modelMgr;
053:
054: public RouteManager(XEditorProject project) {
055: super (project);
056:
057: if (project.getObject(Constants.SERVER_DATASETS) == null) {
058: DebugLogger
059: .logError("You must define a value for the server side routes and service configuration in the startup properties to use XUI's routes and servives");
060: project
061: .fixError(
062: getClass(),
063: "com.xoetrope.carousel.debug.selfhealing.ServerPropertiesMissing",
064: this , null);
065: }
066:
067: loadRouteDefs(currentProject);
068: loadRoutesFile(true);
069: loadRoutesFile(false);
070: modelMgr = new ModelManager(project);
071: }
072:
073: public ModelManager getModelMgr() {
074: return modelMgr;
075: }
076:
077: /**
078: * Setup and instance of the route manager for a project
079: * @param project the current project
080: */
081: //public static XRouteManager setupRouteManager( XProject project )
082: //{
083: /*RouteManager routeManager = ( ( RouteManager ) project.getObject( "RouteMgr" ) );
084: if (( routeManager == null ) || !( routeManager instanceof RouteManager ) ) {
085: routeManager = new RouteManager();
086: project.setObject( "RouteMgr", routeManager );
087: routeManager.setProject( project );
088: routeManager.loadRouteDefs( project );
089: routeManager.loadRoutesFile( true );
090: routeManager.loadRoutesFile( false );
091: }
092: return routeManager;
093: */
094: //}
095: /*private void setProject( XProject proj )
096: {
097: project = proj;
098: }*/
099:
100: private void loadRouteDefs(XProject project) {
101: try {
102: URL url = getClass()
103: .getClassLoader()
104: .getResource(
105: "com/xoetrope/carousel/resources/routes/routedefs.xml");
106: InputStream is = url.openStream();
107: BufferedInputStream bis = new BufferedInputStream(is);
108: InputStreamReader isr = new InputStreamReader(bis);
109: XmlElement ele = XmlSource.read(isr);
110: Vector routes = ele.getChildren();
111: routeDefsMdl = new XBaseModel();
112: for (int route = 0; route < routes.size(); route++) {
113: XmlElement eleRoute = (XmlElement) routes.get(route);
114: String name = eleRoute.getAttribute("name");
115: String className = eleRoute.getAttribute("class");
116: String routeType = eleRoute.getAttribute("type");
117: Vector attribs = eleRoute.getChildren("attribs");
118: //String[] props = new String[ 0 ];
119: XBaseModel routeMdl = new XBaseModel(routeDefsMdl);
120: routeMdl.setId(className);
121: XModelHelper.setAttrib(routeMdl, "type", routeType);
122: routeDefsMdl.append(routeMdl);
123: if (attribs != null && attribs.size() > 0) {
124: XBaseModel attribsMdl = new XBaseModel(routeMdl);
125: attribsMdl.setId("attribs");
126: routeMdl.append(attribsMdl);
127: Vector attriblist = ((XmlElement) attribs.get(0))
128: .getChildren();
129: for (int attrib = 0; attrib < attriblist.size(); attrib++) {
130: XmlElement attribEle = (XmlElement) attriblist
131: .get(attrib);
132: XBaseModel attribMdl = new XBaseModel(
133: attribsMdl);
134: attribMdl.setId(attribEle.getAttribute("name"));
135: attribsMdl.append(attribMdl);
136: }
137: }
138: }
139: } catch (Exception e) {
140: e.printStackTrace();
141: }
142: }
143:
144: public void loadRoutesFile(boolean server) {
145: XEditorProject proj = (XEditorProject) XEditorProjectManager
146: .getCurrentProject();
147: XBaseModel targetMdl = null;
148: String fileName = null;
149: String path = null;
150: if (server) {
151: if (currentProject.getObject(Constants.SERVER_ROUTES) != null) {
152: fileName = (String) ((XEditorProjectProperty) currentProject
153: .getObject(Constants.SERVER_ROUTES)).property;
154: path = proj.getPath() + File.separator + "resources"
155: + File.separator + "server" + File.separator
156: + fileName;
157: if (serverRoutesMdl == null)
158: serverRoutesMdl = new XBaseModel();
159: targetMdl = serverRoutesMdl;
160: }
161: } else if (currentProject.getObject(Constants.ROUTES) != null) {
162: fileName = (String) ((XEditorProjectProperty) currentProject
163: .getObject(Constants.ROUTES)).property;
164: path = proj.getPath() + File.separator + "resources"
165: + File.separator + fileName;
166: if (clientRoutesMdl == null)
167: clientRoutesMdl = new XBaseModel();
168: targetMdl = clientRoutesMdl;
169: }
170:
171: if (fileName != null) {
172: XOptionalDataSource xlibDS = new XOptionalDataSource(
173: currentProject);
174: try {
175: FileReader fr = new FileReader(path);
176: XmlElement ele = XmlSource.read(fr);
177: if (ele != null)
178: xlibDS.loadTable(ele, targetMdl);
179: } catch (Exception ex) {
180: }
181: } else if (serverRoutesMdl == null) {
182: serverRoutesMdl = new XBaseModel();
183: serverRoutesMdl.setTagName(Constants.ROUTES);
184: }
185: }
186:
187: public void saveRoutes() {
188: System.out.println("RouteManager - saveRoutes()");
189: createServerDir();
190: XEditorProjectProperty prop = (XEditorProjectProperty) currentProject
191: .getObject(Constants.ROUTES);
192: String filename = (String) prop.property;
193: if (filename != null) {
194: saveRoutesFile(filename);
195: saveCustomRouteDefs();
196: }
197: createServerStartup();
198: createServerDatasets();
199: saveClientDatasources();
200: }
201:
202: /**
203: * @todo this is an inapproriate and inefficient way to save this file - you
204: * don't know the state of the file that is being read
205: */
206: private void saveClientDatasources() {
207: String dataSourceFile = currentProject
208: .getStartupParam(Constants.MODEL_DATA);
209: StringBuffer contents = new StringBuffer();
210: try {
211: String path = ((XEditorProject) currentProject).getPath()
212: + File.separator + "resources" + File.separator
213: + dataSourceFile;
214: BufferedReader br = new BufferedReader(
215: new InputStreamReader(new FileInputStream(path),
216: "UTF8"));
217: NanoXmlParser nxp = new NanoXmlParser();
218: XmlElement xml = nxp.parse(br);
219:
220: // Create the routes node in the datasources file
221: XEditorProjectProperty routeProp = (XEditorProjectProperty) currentProject
222: .getObject(Constants.ROUTES);
223: String routesFile = (String) routeProp.property;
224: if (routesFile != null) {
225: XmlElement routesElement = findElement(xml,
226: Constants.DATASOURCE, "routes");
227: routesElement.setAttribute("type", "routing");
228: routesElement.setAttribute("filename", routesFile);
229: }
230:
231: // Create the routes node in the datasources file
232: XEditorProjectProperty serviceProp = (XEditorProjectProperty) currentProject
233: .getObject(Constants.SERVICES);
234: String serviceFile = (String) serviceProp.property;
235: if (serviceFile != null) {
236: XmlElement servicesElement = xml
237: .createElement(Constants.DATASOURCE);
238: servicesElement.setAttribute("type", "service");
239: servicesElement.setAttribute("filename", serviceFile);
240: }
241:
242: FileWriter fw = new FileWriter(path);
243: XmlWriter xmlWriter = new NanoXmlWriter(fw);
244: xmlWriter.write(xml, true, 4);
245: fw.flush();
246: fw.close();
247:
248: } catch (Exception ex) {
249: ex.printStackTrace();
250: }
251: }
252:
253: /**
254: * Find a matching child element. If the child is not present it is created
255: * @param xml the xml model
256: * @param type the type of lement to find
257: * @id the id value of the required node
258: * @return the matching new or existing node
259: */
260: private XmlElement findElement(XmlElement xml, String type,
261: String id) {
262: Vector children = xml.getChildren();
263: for (Object c : children) {
264: XmlElement child = (XmlElement) c;
265: String tag = child.getName();
266: if ((tag != null) && tag.equals(type)) {
267: String childId = child.getAttribute("id");
268: if ((childId != null) && childId.equals(id)) {
269: return child;
270: }
271: }
272: }
273:
274: XmlElement child = xml.createElement(type);
275: child.setAttribute("id", id);
276: xml.addChild(child);
277: return child;
278: }
279:
280: private void createServerDir() {
281: String path = ((XEditorProject) currentProject).getPath()
282: + File.separator + "resources" + File.separator
283: + "server";
284: File file = new File(path);
285: if (!file.exists())
286: file.mkdir();
287: }
288:
289: public void saveRoutes(String file, boolean server) {
290: String fileName = null;
291: XBaseModel targetMdl = null;
292: String projectPath = ((XEditorProject) currentProject)
293: .getPath();
294: String path = null;
295: if (server) {
296: if (currentProject.getObject(Constants.SERVER_ROUTES) != null) {
297: fileName = (String) ((XEditorProjectProperty) currentProject
298: .getObject(Constants.SERVER_ROUTES)).property;
299: path = projectPath + File.separator + "resources"
300: + File.separator + "server" + File.separator
301: + fileName;
302: targetMdl = serverRoutesMdl;
303: }
304: } else if (currentProject.getObject(Constants.ROUTES) != null) {
305: fileName = (String) ((XEditorProjectProperty) currentProject
306: .getObject(Constants.ROUTES)).property;
307: path = projectPath + File.separator + "resources"
308: + File.separator + fileName;
309: targetMdl = clientRoutesMdl;
310: }
311:
312: if (path != null) {
313: try {
314: FileWriter fw = new FileWriter(path);
315: XDataSource.outputModel(fw, targetMdl);
316: fw.flush();
317: fw.close();
318: } catch (Exception e) {
319: e.printStackTrace();
320: }
321: }
322: }
323:
324: private void createServerStartup() {
325: XEditorProjectProperty prop = (XEditorProjectProperty) currentProject
326: .getObject(Constants.SERVER_STARTUP);
327: String filename = (String) prop.property;
328: Properties props = new Properties();
329: String path = ((XEditorProject) currentProject).getPath();
330: path += File.separator + "resources" + File.separator
331: + "server" + File.separator + filename;
332: File file = new File(path);
333: try {
334: if (file.exists()) {
335: FileInputStream fis = new FileInputStream(file);
336: props.load(fis);
337: fis.close();
338: }
339: prop = (XEditorProjectProperty) currentProject
340: .getObject(Constants.SERVER_DATASETS);
341: String serverDatasets = (String) prop.property;
342: props.setProperty(Constants.MODEL_DATA, serverDatasets);
343: FileOutputStream fos = new FileOutputStream(path);
344: props.store(fos, null);
345: fos.flush();
346: fos.close();
347: } catch (Exception ex) {
348: ex.printStackTrace();
349: }
350: }
351:
352: private void createServerDatasets() {
353: XBaseModel sources = new XBaseModel();
354: sources.setTagName(Constants.DATASOURCES);
355: XBaseModel child = new XBaseModel(sources);
356: child.setTagName(Constants.DATASOURCE);
357: XModelHelper.setAttrib(child, "name", Constants.ROUTES);
358: XModelHelper.setAttrib(child, "type", "routing");
359: XEditorProjectProperty prop = (XEditorProjectProperty) currentProject
360: .getObject(Constants.SERVER_ROUTES);
361: String value = (String) prop.property;
362: XModelHelper.setAttrib(child, "filename", value);
363: sources.append(child);
364:
365: child = new XBaseModel(sources);
366: child.setTagName(Constants.DATASOURCE);
367: XModelHelper.setAttrib(child, "name", "Service");
368: XModelHelper.setAttrib(child, "type", "service");
369: prop = (XEditorProjectProperty) currentProject
370: .getObject(Constants.SERVER_SERVICES);
371: value = (String) prop.property;
372: XModelHelper.setAttrib(child, "filename", value);
373: sources.append(child);
374:
375: prop = (XEditorProjectProperty) currentProject
376: .getObject(Constants.SERVER_DATASETS);
377: value = (String) prop.property;
378: String path = ((XEditorProject) currentProject).getPath()
379: + File.separator + "resources" + File.separator
380: + "server";
381: path += File.separator + value;
382: try {
383: FileWriter fw = new FileWriter(path);
384: XDataSource.outputModel(fw, sources);
385: fw.flush();
386: fw.close();
387: } catch (Exception ex) {
388: ex.printStackTrace();
389: }
390: }
391:
392: public void createServerRoute(String name) {
393: XBaseModel newMdl = (XBaseModel) serverRoutesMdl.get(name);
394: newMdl.setTagName("Route");
395: }
396:
397: public void createRouteLayer(String routeName, String serviceLayer,
398: int side) {
399: XBaseModel targetMdl = getTarget(side);
400: XBaseModel routeMdl = (XBaseModel) targetMdl.get(routeName);
401: routeMdl.setTagName("route");
402: XBaseModel layerMdl = (XBaseModel) routeMdl.get(serviceLayer);
403: layerMdl.setTagName("layer");
404: XModelHelper.setAttrib(layerMdl, "class", serviceLayer);
405: }
406:
407: public void setServerLayerAttrib(String routeName,
408: String serviceLayer, String attribName, String attribValue) {
409: XBaseModel layerMdl = (XBaseModel) serverRoutesMdl
410: .get(routeName + "/" + serviceLayer);
411: XModelHelper.setAttrib(layerMdl, attribName, attribValue);
412: }
413:
414: public void removeRoute(String routeName, int side) {
415: XBaseModel targetMdl = getTarget(side);
416: XBaseModel routeMdl = (XBaseModel) targetMdl.get(routeName);
417: ((XBaseModel) routeMdl.getParent()).remove(routeMdl);
418: }
419:
420: public void changeServerRouteName(int side, String oldName,
421: String newName) {
422: XBaseModel targetMdl = getTarget(side);
423: XBaseModel routeMdl = (XBaseModel) targetMdl.get(oldName);
424: routeMdl.setId(newName);
425: }
426:
427: private XBaseModel getTarget(int side) {
428: if (side == RouteDataEditor.SIDE_CLIENT)
429: return clientRoutesMdl;
430: else
431: return serverRoutesMdl;
432: }
433:
434: public XBaseModel getRoutes(int side) {
435: return getTarget(side);
436: }
437:
438: public XBaseModel getRouteDefs() {
439: return routeDefsMdl;
440: }
441:
442: public XModel createCustomRouteDefs() {
443: customRouteDefsMdl = new XBaseModel();
444: customRouteDefsMdl.setId("RouteDefs");
445: customRouteDefsMdl.setTagName("RouteDefs");
446: return customRouteDefsMdl;
447: }
448:
449: public void addCustomRouteService(String className, String baseClass) {
450: addCustomRouteService(className, baseClass, true, null);
451: }
452:
453: public void addCustomRouteService(String className,
454: String baseClass, String[] customAttribs) {
455: addCustomRouteService(className, baseClass, true, customAttribs);
456: }
457:
458: public void addCustomRouteService(String className,
459: String baseClass, boolean loadCustom, String[] customAttribs) {
460: baseClass = (baseClass == null) ? className : baseClass;
461: XBaseModel newRouteMdl = (XBaseModel) customRouteDefsMdl
462: .get(className);
463: if (loadCustom) {
464: XModelHelper.setAttrib(newRouteMdl, "class", className);
465: if ((baseClass != null)
466: && (baseClass.compareTo(className) != 0))
467: XModelHelper.setAttrib(newRouteMdl, "baseclass",
468: baseClass);
469: newRouteMdl.setTagName("Route");
470: }
471:
472: XBaseModel baseMdl = (XBaseModel) routeDefsMdl.get(baseClass);
473: XBaseModel newBaseMdl = (XBaseModel) routeDefsMdl
474: .get(className);
475: if (baseMdl != null) {
476: String type = XModelHelper.getAttrib(baseMdl, "type");
477: if (type != null && type.compareTo("abstract") != 0)
478: XModelHelper.setAttrib(newBaseMdl, "type", type);
479:
480: if (baseClass != null)
481: XModelHelper
482: .setAttrib(newBaseMdl, "extends", baseClass);
483:
484: if (loadCustom)
485: addAttribs(baseMdl, newRouteMdl);
486:
487: addAttribs(baseMdl, newBaseMdl);
488: }
489: addCustomAttribs(newRouteMdl, customAttribs);
490: addCustomAttribs(newBaseMdl, customAttribs);
491: }
492:
493: private void addAttribs(XBaseModel src, XBaseModel target) {
494: if (src.getNumChildren() > 0) {
495: XBaseModel attribMdl = (XBaseModel) src.get(0);
496: XBaseModel newRouteAttribs = (XBaseModel) target
497: .get("attribs");
498: if (target != null) {
499: newRouteAttribs.setTagName("attribs");
500: for (int i = 0; i < attribMdl.getNumChildren(); i++) {
501: String name = attribMdl.get(i).getId();
502: XBaseModel newRouteAtt = (XBaseModel) newRouteAttribs
503: .get(name);
504: newRouteAtt.setTagName("attrib");
505: XModelHelper.setAttrib(newRouteAtt, "name", name);
506: }
507: }
508: }
509: }
510:
511: private void addCustomAttribs(XBaseModel target,
512: String[] customAttribs) {
513: if (customAttribs != null) {
514: XBaseModel newRouteAttribs = (XBaseModel) target
515: .get("attribs");
516: for (int i = 0; i < customAttribs.length; i++) {
517: XBaseModel newRouteAtt = (XBaseModel) newRouteAttribs
518: .get(customAttribs[i]);
519: newRouteAtt.setTagName("attrib");
520: XModelHelper.setAttrib(newRouteAtt, "name",
521: customAttribs[i]);
522: }
523: }
524: }
525:
526: /**
527: * Move a route
528: * @param routeName the name of the route
529: * @param layerName the layer to move
530: * @param moveUp true to move the layer up, if possible
531: */
532: public void moveLayer(String routeName, String layerName,
533: boolean moveUp, boolean server) {
534: XBaseModel targetMdl;
535: if (server)
536: targetMdl = serverRoutesMdl;
537: else
538: targetMdl = clientRoutesMdl;
539:
540: XBaseModel routeMdl = (XBaseModel) targetMdl.get(routeName);
541: XBaseModel tempMdl = (XBaseModel) XModelHelper.clone(routeMdl);
542: routeMdl.clear();
543: XBaseModel layerMdl = (XBaseModel) tempMdl.get(layerName);
544: int pos = XModelHelper.getPositionInModel(layerMdl);
545: int newPos = moveUp ? pos - 1 : pos + 1;
546: for (int i = 0; i < tempMdl.getNumChildren(); i++) {
547: XBaseModel child;
548: if (i == pos)
549: child = (XBaseModel) tempMdl.get(newPos);
550: else if (i == newPos)
551: child = (XBaseModel) tempMdl.get(pos);
552: else
553: child = (XBaseModel) tempMdl.get(i);
554:
555: XBaseModel layer = (XBaseModel) XModelHelper.clone(child);
556: layer.setTagName("layer");
557: routeMdl.append(layer);
558: }
559: }
560:
561: /**
562: * Get the name of the routes file from the XLibXModelManager and load the file
563: * from from the resources directory. Call the XLibDataSource object with the
564: * root element
565: * @param currentProject the project we are working with
566: */
567: void loadRoutesFromFile(XEditorProject currentProject) {
568: XOptionalDataSource xlibDS = new XOptionalDataSource(
569: currentProject);
570:
571: String routingFilename = modelMgr.getRoutingFileName();
572: if (routingFilename != null) {
573: routingFilename = currentProject.getPath() + File.separator
574: + "resources" + File.separator + routingFilename;
575: FileReader fr = null;
576: try {
577: fr = new FileReader(routingFilename);
578: } catch (FileNotFoundException ex) {
579: }
580:
581: if (fr != null) {
582: XmlElement ele = XmlSource.read(fr);
583: if (ele != null)
584: xlibDS.loadRoutes(ele, currentProject.getModel());
585: }
586: }
587: }
588:
589: /**
590: * Iterate the routes in the routes hashtable and build the xml file. Each
591: * Routes layers also needs to be iterated in order to add to the file. Save
592: * the constructed xml in the specified file.
593: * @param project the project we are working with
594: * @param filename the path to the routes file.
595: */
596: void saveRoutesFile(String filename) {
597: saveRoutes(filename, false);
598: saveRoutes(null, true);
599: }
600:
601: private void saveFile(XmlElement ele, String path) {
602: try {
603: FileOutputStream fos = new FileOutputStream(path);
604: NanoXmlWriter writer = new NanoXmlWriter(fos);
605: writer.write(ele, true, 4);
606: fos.flush();
607: fos.close();
608: } catch (IOException ex) {
609: ex.printStackTrace();
610: }
611:
612: }
613:
614: private XmlElement genServerRoutes(NanoXmlElement eleRoutes) {
615: NanoXmlElement eleServerRoutes = new NanoXmlElement(
616: Constants.ROUTES);
617: Vector routesV = eleRoutes.getChildren();
618: for (int i = 0; i < routesV.size(); i++) {
619: XmlElement eleCliRoute = (XmlElement) routesV.get(i);
620: String id = eleCliRoute.getAttribute("id");
621: NanoXmlElement eleServerRoute = new NanoXmlElement("Route");
622: eleServerRoutes.addChild(eleServerRoute);
623: eleServerRoute.setAttribute("id", id);
624: Vector layersV = eleCliRoute.getChildren();
625: for (int layer = layersV.size() - 1; layer >= 0; layer--) {
626: NanoXmlElement eleCliLayer = (NanoXmlElement) layersV
627: .get(layer);
628: String type = getLayerType(eleCliLayer
629: .getAttribute("class"));
630: type = type == null ? "" : type;
631: if (type.compareTo("clientEndPoint") != 0) {
632: NanoXmlElement eleServerLayer = new NanoXmlElement(
633: "layer");
634: IXMLElement iEle = (IXMLElement) eleCliLayer
635: .getImplementation();
636: Properties attribs = iEle.getAttributes();
637: Enumeration enumKeys = attribs.keys();
638: while (enumKeys.hasMoreElements()) {
639: String attName = (String) enumKeys
640: .nextElement();
641: String attValue = (String) attribs.get(attName);
642: eleServerLayer.setAttribute(attName, attValue);
643: }
644: eleServerRoute.addChild(eleServerLayer);
645: }
646: }
647: }
648: return eleServerRoutes;
649: }
650:
651: private String getLayerType(String name) {
652: XBaseModel mdl = (XBaseModel) routeDefsMdl.get(name);
653: return XModelHelper.getAttrib(mdl, "type");
654: }
655:
656: public void saveCustomRouteDefs() {
657: if (customRouteDefsMdl != null) {
658: XEditorProjectProperty prop = (XEditorProjectProperty) currentProject
659: .getObject("CustomRouteDefs");
660: String filename = (String) prop.property;
661: String path = ((XEditorProject) currentProject).getPath()
662: + File.separator + filename;
663:
664: try {
665: FileWriter fw = new FileWriter(path);
666: XDataSource.outputModel(fw, customRouteDefsMdl);
667: fw.flush();
668: fw.close();
669: } catch (Exception e) {
670: e.printStackTrace();
671: }
672: }
673: }
674:
675: public void openCustomRouteDefs(File file) {
676: try {
677: FileReader sr = new FileReader(file);
678: XmlElement src = XmlSource.read(sr);
679: XOptionalDataSource dataSource = new XOptionalDataSource(
680: currentProject);
681: dataSource.loadTable(src, customRouteDefsMdl);
682: for (int i = 0; i < customRouteDefsMdl.getNumChildren(); i++) {
683: XBaseModel child = (XBaseModel) customRouteDefsMdl
684: .get(i);
685: String className = XModelHelper.getAttrib(child,
686: "class");
687: String baseclass = XModelHelper.getAttrib(child,
688: "baseclass");
689: String[] customAttribs = null;
690: if (child.getNumChildren() > 0) {
691: XBaseModel attribs = (XBaseModel) child
692: .get("attribs");
693: customAttribs = new String[attribs.getNumChildren()];
694: for (int att = 0; att < attribs.getNumChildren(); att++) {
695: customAttribs[att] = attribs.get(att).getId();
696: }
697: }
698: addCustomRouteService(className, baseclass, false,
699: customAttribs);
700: }
701: } catch (Exception e) {
702: e.printStackTrace();
703: }
704: }
705:
706: public XBaseModel getCustomRoutes() {
707: return customRouteDefsMdl;
708: }
709: }
|