001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Nov 2, 2005
014: * @author dmoran
015: */
016:
017: package org.pentaho.designstudio.editors.actionsequence;
018:
019: import java.util.List;
020:
021: import org.dom4j.Document;
022: import org.dom4j.Element;
023: import org.pentaho.actionsequence.dom.AbstractIOElement;
024: import org.pentaho.actionsequence.dom.ActionControlStatement;
025: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
026: import org.pentaho.actionsequence.dom.IActionSequenceDocumentListener;
027: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
028: import org.pentaho.designstudio.controls.XmlModificationEvent;
029: import org.pentaho.designstudio.editors.xmleditor.XMLDocumentProvider;
030: import org.pentaho.designstudio.messages.Messages;
031: import org.pentaho.designstudio.util.XmlHelper;
032:
033: public class ActionSequenceDocumentProvider extends XMLDocumentProvider
034: implements IActionSequenceDocumentListener {
035: ActionSequenceDocument actionSequenceDocument;
036:
037: private static String[] nodesToCheck = {
038: ActionSequenceDocument.ACTION_SEQUENCE,
039: ActionSequenceDocument.ACTION_SEQUENCE
040: + "/" + ActionSequenceDocument.ACTIONS_NAME, //$NON-NLS-1$
041: };
042:
043: public String getDocErrors() {
044: String rtn = super .getDocErrors();
045: if (rtn == null) {
046: rtn = checkNodes();
047: }
048: return (rtn);
049: }
050:
051: private String checkNodes() {
052: StringBuffer rtn = new StringBuffer();
053: String errorHeader = Messages
054: .getString("ActionSequenceDocumentProvider.UI_XML_ERROR_MSG"); //$NON-NLS-1$
055: Document doc = getDocument();
056:
057: if (doc != null) {
058: for (int i = 0; i < nodesToCheck.length; ++i) {
059: if (doc.selectSingleNode(nodesToCheck[i]) == null) {
060: rtn.append("\n").append(nodesToCheck[i]); //$NON-NLS-1$
061: }
062: }
063:
064: if (rtn.length() > 0) {
065: return (errorHeader + rtn.toString());
066: }
067: } else {
068: return (errorHeader);
069: }
070:
071: return (null);
072: }
073:
074: public Document prettyPrint(Document doc) {
075:
076: // Sort the header nodes
077: XmlHelper.sortNodes(doc.getRootElement(), new String[] {
078: ActionSequenceDocument.ACTION_SEQUENCE_NAME,
079: ActionSequenceDocument.ACTION_SEQUENCE_TITLE,
080: ActionSequenceDocument.ACTION_SEQUENCE_VERSION,
081: ActionSequenceDocument.ACTION_SEQUENCE_LOGGING_LEVEL,
082: ActionSequenceDocument.ACTION_SEQUENCE_DOCUMENTATION,
083: ActionSequenceDocument.DOC_INPUTS_NAME,
084: ActionSequenceDocument.DOC_OUTPUTS_NAME,
085: ActionSequenceDocument.DOC_RESOURCES_NAME, });
086:
087: // Sort the action-definition nodes
088: List nodeList = doc
089: .selectNodes("//" + ActionSequenceDocument.ACTION_DEFINITION_NAME); //$NON-NLS-1$
090: for (int i = 0; i < nodeList.size(); ++i) {
091: Element ele = (Element) nodeList.get(i);
092:
093: XmlHelper.sortNodes(ele, new String[] {
094: ActionSequenceDocument.COMPONENT_NAME,
095: ActionSequenceDocument.ACTION_TYPE_NAME,
096: ActionSequenceDocument.ACTION_INPUTS_NAME,
097: ActionSequenceDocument.ACTION_RESOURCES_NAME,
098: ActionSequenceDocument.ACTION_OUTPUTS_NAME,
099: ActionSequenceDocument.COMPONENT_DEF_NAME, });
100: }
101:
102: // Format the XML nicely
103: Document newDoc = XmlHelper.prettyPrint(doc);
104: if (newDoc == null) {
105: return (doc);
106: }
107: doc = newDoc;
108:
109: // Add some extra newlines to break apart sections
110: Element top = doc.getRootElement();
111: XmlHelper.appendNL(top,
112: ActionSequenceDocument.ACTION_SEQUENCE_DOCUMENTATION);
113: XmlHelper.appendNL(top, ActionSequenceDocument.DOC_INPUTS_NAME);
114: XmlHelper
115: .appendNL(top, ActionSequenceDocument.DOC_OUTPUTS_NAME);
116: XmlHelper.appendNL(top,
117: ActionSequenceDocument.DOC_RESOURCES_NAME);
118:
119: nodeList = doc
120: .selectNodes("//" + ActionSequenceDocument.ACTIONS_NAME); //$NON-NLS-1$
121: for (int i = 0; i < nodeList.size(); ++i) {
122: Element ele = (Element) nodeList.get(i);
123: XmlHelper
124: .appendNL(ele, ActionSequenceDocument.ACTIONS_NAME);
125: XmlHelper.appendNL(ele,
126: ActionSequenceDocument.ACTION_DEFINITION_NAME);
127: }
128:
129: return (doc);
130: }
131:
132: protected void updateXmlDocument() {
133: if (actionSequenceDocument != null) {
134: actionSequenceDocument.removeListener(this );
135: }
136: super .updateXmlDocument();
137: Document document = getDocument();
138: if ((document != null)
139: && document.getRootElement().getName().equals(
140: ActionSequenceDocument.ACTION_SEQUENCE)) {
141: actionSequenceDocument = new ActionSequenceDocument(
142: getDocument());
143: actionSequenceDocument.addListener(this );
144: fireXmlModificationEvent(new XmlModificationEvent(this ,
145: actionSequenceDocument.getDocument(),
146: XmlModificationEvent.NODE_VALUE_CHANGED));
147: } else {
148: actionSequenceDocument = null;
149: }
150: }
151:
152: public void prettyPrint() {
153: if (actionSequenceDocument != null) {
154: actionSequenceDocument.removeListener(this );
155: }
156: super .prettyPrint();
157: if (getDocument() != null) {
158: actionSequenceDocument = new ActionSequenceDocument(
159: getDocument());
160: actionSequenceDocument.addListener(this );
161: fireXmlModificationEvent(new XmlModificationEvent(this ,
162: actionSequenceDocument.getDocument(),
163: XmlModificationEvent.NODE_VALUE_CHANGED));
164: } else {
165: actionSequenceDocument = null;
166: }
167: }
168:
169: public ActionSequenceDocument getActionSequence() {
170: return actionSequenceDocument;
171: }
172:
173: public void actionAdded(ActionDefinition action) {
174: setChanged();
175: }
176:
177: public void actionChanged(ActionDefinition action) {
178: setChanged();
179: }
180:
181: public void actionRemoved(Object parent, ActionDefinition action) {
182: setChanged();
183: }
184:
185: public void actionRenamed(ActionDefinition action) {
186: setChanged();
187: }
188:
189: public void ioAdded(AbstractIOElement io) {
190: setChanged();
191: }
192:
193: public void ioChanged(AbstractIOElement io) {
194: setChanged();
195: }
196:
197: public void ioRemoved(Object parent, AbstractIOElement io) {
198: setChanged();
199: }
200:
201: public void ioRenamed(AbstractIOElement io) {
202: setChanged();
203: }
204:
205: public void controlStatementAdded(
206: ActionControlStatement controlStatement) {
207: setChanged();
208: }
209:
210: public void controlStatementChanged(
211: ActionControlStatement controlStatement) {
212: setChanged();
213: }
214:
215: public void controlStatementRemoved(Object parent,
216: ActionControlStatement controlStatement) {
217: setChanged();
218: }
219:
220: public void resourceAdded(Object resource) {
221: setChanged();
222: }
223:
224: public void resourceChanged(Object resource) {
225: setChanged();
226: }
227:
228: public void resourceRemoved(Object parent, Object resource) {
229: setChanged();
230: }
231:
232: public void resourceRenamed(Object resource) {
233: setChanged();
234: }
235:
236: public void headerChanged(
237: ActionSequenceDocument actionSequenceDocument) {
238: setChanged();
239: }
240:
241: }
|