001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.xml.export;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.Writer;
023: import java.util.Iterator;
024: import java.util.Set;
025:
026: import de.finix.contelligent.Component;
027: import de.finix.contelligent.ComponentManager;
028: import de.finix.contelligent.ComponentNotFoundException;
029: import de.finix.contelligent.ComponentPath;
030: import de.finix.contelligent.Container;
031: import de.finix.contelligent.components.SortedFolder;
032: import de.finix.contelligent.core.ComponentManagerInternal;
033: import de.finix.contelligent.core.ContelligentImpl;
034: import de.finix.contelligent.logging.LoggingService;
035: import de.finix.contelligent.util.ThreadedMem;
036:
037: /**
038: * The default {@link ComponentXMLPolicy policy} for writing an XML
039: * representation of a component or a component-tree. Any binary content may be
040: * included into the XML or written to a separate file.
041: */
042: public class DefaultComponentXMLPolicy extends SingleComponentXMLPolicy {
043:
044: final static org.apache.log4j.Logger log = LoggingService
045: .getLogger(DefaultComponentXMLPolicy.class);
046:
047: protected ComponentManager componentManager;
048:
049: /**
050: * Creates a new <code>DefaultComponentXMLPolicy</code> instance. If
051: * <tt>dir</tt> is not null any binary resource is written into a separate
052: * file Note that any content writing is omited if {@link #includeContent}
053: * yields false.
054: *
055: * @param componentManager
056: * a <code>ComponentManager</code> value
057: * @param dir
058: * a <code>File</code> value
059: */
060: public DefaultComponentXMLPolicy(ComponentManager componentManager,
061: File dir) {
062: super (dir);
063: this .componentManager = componentManager;
064: }
065:
066: /**
067: * Creates a new <code>DefaultComponentXMLPolicy</code> instance. No
068: * binary resources are written.
069: *
070: * @param componentManager
071: * a <code>ComponentManager</code> value
072: */
073: public DefaultComponentXMLPolicy(ComponentManager componentManager) {
074: this (componentManager, null);
075: }
076:
077: protected Set writeSubcomponents(Component component, Writer writer)
078: throws IOException, ComponentNotFoundException {
079: if (currentDepth > maxDepth()) {
080: return null;
081: }
082: Set result = null;
083: if (component instanceof Container) {
084: if (log.isDebugEnabled())
085: log.debug("'" + componentManager.getName()
086: + " ... checking container '" + component
087: + "' for subcomponents ...");
088:
089: // export all subcomponents (recursivly)
090: Container container = (Container) component;
091: Iterator subNames = container.getSubcomponentNames();
092:
093: while (subNames.hasNext()) {
094: ComponentPath subComponentName = new ComponentPath(
095: (String) subNames.next());
096: try {
097: log.debug(" ... current subcomponent is '"
098: + subComponentName + "' ...");
099: Component sub = componentManager.getSubcomponent(
100: container, subComponentName, ThreadedMem
101: .getCallData(), followLinks());
102: Set s = writeComponent(sub, writer);
103: if (s != null) {
104: if (result == null)
105: result = s;
106: else
107: result.addAll(s);
108: }
109: } catch (ComponentNotFoundException cne) {
110: // Probably a bad name list; try to fix it, otherwise log
111: // and ignore.
112: log
113: .warn(
114: "Received a subcomponent name that could not be found.",
115: cne);
116: try {
117: if ((component instanceof SortedFolder)
118: && (componentManager.isRoot())
119: && (!ContelligentImpl.getInstance()
120: .isReadOnly())) {
121: ((ComponentManagerInternal) componentManager)
122: .cleanMissingSubcomponent(
123: (SortedFolder) container,
124: subComponentName.toString());
125: }
126: } catch (Exception e) {
127: log
128: .error(
129: "Unable to fix invalid subcomponent name.",
130: e);
131: }
132: }
133: }
134: }
135: return result;
136: }
137:
138: protected int maxDepth() {
139: return Integer.MAX_VALUE;
140: }
141:
142: }
|