001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.xml.export;
018:
019: import java.io.StringReader;
020: import java.util.Iterator;
021:
022: import org.apache.log4j.Logger;
023: import org.jdom.Element;
024: import org.jdom.Namespace;
025:
026: import edu.iu.uis.eden.KEWServiceLocator;
027: import edu.iu.uis.eden.edl.EDocLiteAssociation;
028: import edu.iu.uis.eden.edl.EDocLiteDefinition;
029: import edu.iu.uis.eden.edl.EDocLiteService;
030: import edu.iu.uis.eden.edl.EDocLiteStyle;
031: import edu.iu.uis.eden.export.ExportDataSet;
032: import edu.iu.uis.eden.util.XmlHelper;
033: import edu.iu.uis.eden.xml.XmlConstants;
034:
035: /**
036: * Exports EDocLite definitions to XML.
037: *
038: * @see EDocLiteDefinition
039: *
040: * @author rkirkend
041: */
042: public class EDocLiteXmlExporter implements XmlExporter, XmlConstants {
043:
044: private static final Logger LOG = Logger
045: .getLogger(EDocLiteXmlExporter.class);
046:
047: private ExportRenderer renderer = new ExportRenderer(EDL_NAMESPACE);
048:
049: public Element export(ExportDataSet dataSet) {
050: if (!dataSet.getEdocLites().isEmpty()) {
051: Element rootElement = renderer.renderElement(null,
052: EDL_EDOCLITE);
053: rootElement.setAttribute(SCHEMA_LOCATION_ATTR,
054: EDL_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
055: for (Iterator iter = dataSet.getEdocLites().iterator(); iter
056: .hasNext();) {
057: EDocLiteAssociation edocLite = (EDocLiteAssociation) iter
058: .next();
059: exportEDocLite(rootElement, edocLite);
060: }
061: return rootElement;
062: }
063: return null;
064: }
065:
066: private void exportEDocLite(Element parentEl,
067: EDocLiteAssociation edl) {
068:
069: try {
070: EDocLiteService edlService = KEWServiceLocator
071: .getEDocLiteService();
072: if (edl.getDefinition() != null) { //this probably shouldn't be supported on the entry side...
073: EDocLiteDefinition def = edlService
074: .getEDocLiteDefinition(edl.getDefinition());
075: if (def == null) {
076: LOG.error("Attempted to export definition "
077: + edl.getDefinition()
078: + " which was not found");
079: return;
080: }
081: Element defEl = XmlHelper.buildJDocument(
082: new StringReader(def.getXmlContent()))
083: .getRootElement();
084: setNamespace(defEl, EDL_NAMESPACE);
085: parentEl.addContent(defEl);
086: }
087:
088: if (edl.getStyle() != null) {//this probably shouldn't be supported on the entry side...
089: Element styleWrapperEl = renderer.renderElement(
090: parentEl, EDL_STYLE);
091: renderer.renderAttribute(styleWrapperEl, "name", edl
092: .getStyle());
093: EDocLiteStyle style = edlService.getEDocLiteStyle(edl
094: .getStyle());
095: if (style == null) {
096: LOG.error("Attempted to export style "
097: + edl.getStyle() + " which was not found");
098: return;
099: }
100: Element styleEl = XmlHelper.buildJDocument(
101: new StringReader(style.getXmlContent()))
102: .getRootElement();
103: styleWrapperEl.addContent(styleEl);
104: }
105:
106: Element associationEl = renderer.renderElement(parentEl,
107: EDL_ASSOCIATION);
108: renderer.renderTextElement(associationEl, EDL_DOC_TYPE, edl
109: .getEdlName());
110: if (edl.getDefinition() != null) {
111: renderer.renderTextElement(associationEl,
112: EDL_DEFINITION, edl.getDefinition());
113: }
114: if (edl.getStyle() != null) {
115: renderer.renderTextElement(associationEl, EDL_STYLE,
116: edl.getStyle());
117: }
118:
119: renderer.renderTextElement(associationEl, EDL_ACTIVE, edl
120: .getActiveInd().toString());
121: } catch (Exception e) {
122: throw new RuntimeException(e);
123: }
124: }
125:
126: private void setNamespace(Element element, Namespace namespace) {
127: element.setNamespace(namespace);
128: for (Iterator iter = element.getChildren().iterator(); iter
129: .hasNext();) {
130: setNamespace((Element) iter.next(), namespace);
131: }
132: }
133: }
|