001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixcore.editor2.core.spring.internal;
020:
021: import java.io.File;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.HashMap;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Set;
032:
033: import org.apache.log4j.Logger;
034: import org.w3c.dom.Document;
035: import org.xml.sax.SAXException;
036:
037: import de.schlund.pfixcore.editor2.core.dom.AbstractTarget;
038: import de.schlund.pfixcore.editor2.core.dom.AbstractTheme;
039: import de.schlund.pfixcore.editor2.core.dom.Image;
040: import de.schlund.pfixcore.editor2.core.dom.IncludePartThemeVariant;
041: import de.schlund.pfixcore.editor2.core.dom.Page;
042: import de.schlund.pfixcore.editor2.core.dom.Project;
043: import de.schlund.pfixcore.editor2.core.dom.Target;
044: import de.schlund.pfixcore.editor2.core.dom.TargetType;
045: import de.schlund.pfixcore.editor2.core.dom.Theme;
046: import de.schlund.pfixcore.editor2.core.dom.ThemeList;
047: import de.schlund.pfixcore.editor2.core.dom.Variant;
048: import de.schlund.pfixcore.editor2.core.exception.EditorIOException;
049: import de.schlund.pfixcore.editor2.core.exception.EditorParsingException;
050: import de.schlund.pfixcore.editor2.core.spring.FileSystemService;
051: import de.schlund.pfixcore.editor2.core.spring.PathResolverService;
052: import de.schlund.pfixcore.editor2.core.spring.ProjectFactoryService;
053: import de.schlund.pfixcore.editor2.core.spring.VariantFactoryService;
054: import de.schlund.pfixxml.targets.AuxDependencyFile;
055: import de.schlund.pfixxml.targets.PageInfo;
056: import de.schlund.pfixxml.targets.TargetDependencyRelation;
057:
058: /**
059: * Implementation of Target using the AuxDependency informationen provided by
060: * the Pustefix generator. This implemenation should only be used for leaf
061: * targets, which are only used as auxiliary dependencies (no XML or XSL
062: * parents).
063: *
064: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
065: */
066: public class TargetAuxDepImpl extends AbstractTarget {
067:
068: private AuxDependencyFile auxdep;
069:
070: private ProjectFactoryService projectfactory;
071:
072: private VariantFactoryService variantfactory;
073:
074: private PathResolverService pathresolver;
075:
076: private FileSystemService filesystem;
077:
078: /**
079: * Creates a Target using an AuxDependency to retrieve information
080: *
081: * @param projectfactory
082: * Reference to ProjectFactoryService
083: * @param variantfactory
084: * Reference to VariantFactoryService
085: * @param auxdep
086: * AuxDependency object to use
087: */
088: public TargetAuxDepImpl(ProjectFactoryService projectfactory,
089: VariantFactoryService variantfactory,
090: PathResolverService pathresolver,
091: FileSystemService filesystem, AuxDependencyFile auxdep) {
092: this .auxdep = auxdep;
093: this .projectfactory = projectfactory;
094: this .variantfactory = variantfactory;
095: this .pathresolver = pathresolver;
096: this .filesystem = filesystem;
097: }
098:
099: /*
100: * (non-Javadoc)
101: *
102: * @see de.schlund.pfixcore.editor2.core.dom.Target#getName()
103: */
104: public String getName() {
105: return this .auxdep.getPath().getRelativePath();
106: }
107:
108: /*
109: * (non-Javadoc)
110: *
111: * @see de.schlund.pfixcore.editor2.core.dom.Target#getType()
112: */
113: public TargetType getType() {
114: return TargetType.TARGET_AUX;
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see de.schlund.pfixcore.editor2.core.dom.Target#getContentXML()
121: */
122: public Document getContentXML() throws EditorIOException,
123: EditorParsingException {
124: File file = new File(this .pathresolver.resolve(auxdep.getPath()
125: .getRelativePath()));
126: Object lock = this .filesystem.getLock(file);
127: synchronized (lock) {
128: try {
129: return this .filesystem.readXMLDocumentFromFile(file);
130: } catch (FileNotFoundException e) {
131: String err = "File " + file.getAbsolutePath()
132: + " could not be found!";
133: Logger.getLogger(this .getClass()).error(err, e);
134: throw new EditorIOException(err, e);
135: } catch (SAXException e) {
136: String err = "Error during parsing file "
137: + file.getAbsolutePath() + "!";
138: Logger.getLogger(this .getClass()).error(err, e);
139: throw new EditorParsingException(err, e);
140: } catch (IOException e) {
141: String err = "File " + file.getAbsolutePath()
142: + " could not be read!";
143: Logger.getLogger(this .getClass()).error(err, e);
144: throw new EditorIOException(err, e);
145: }
146: }
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see de.schlund.pfixcore.editor2.core.dom.Target#getParentXML()
153: */
154: public Target getParentXML() {
155: // Leaf targets don't have parents
156: return null;
157: }
158:
159: /*
160: * (non-Javadoc)
161: *
162: * @see de.schlund.pfixcore.editor2.core.dom.Target#getParentXSL()
163: */
164: public Target getParentXSL() {
165: // Leaf targets don't have parents
166: return null;
167: }
168:
169: /*
170: * (non-Javadoc)
171: *
172: * @see de.schlund.pfixcore.editor2.core.dom.Target#getAuxDependencies()
173: */
174: public Collection<Target> getAuxDependencies() {
175: // Leaf targets don't have dependencies
176: return new ArrayList<Target>();
177: }
178:
179: /*
180: * (non-Javadoc)
181: *
182: * @see de.schlund.pfixcore.editor2.core.dom.Target#getIncludeDependencies(boolean)
183: */
184: public Collection<IncludePartThemeVariant> getIncludeDependencies(
185: boolean recursive) {
186: // Leaf targets don't have dependencies
187: return new ArrayList<IncludePartThemeVariant>();
188: }
189:
190: /*
191: * (non-Javadoc)
192: *
193: * @see de.schlund.pfixcore.editor2.core.dom.Target#getImageDependencies(boolean)
194: */
195: public Collection<Image> getImageDependencies(boolean recursive) {
196: // Leaf targets don't have dependencies
197: return new ArrayList<Image>();
198: }
199:
200: /*
201: * (non-Javadoc)
202: *
203: * @see de.schlund.pfixcore.editor2.core.dom.Target#getAffectedPages()
204: */
205: public Collection<Page> getAffectedPages() {
206: HashSet<PageInfo> pageinfos = new HashSet<PageInfo>();
207: HashSet<Page> pages = new HashSet<Page>();
208: Set<de.schlund.pfixxml.targets.Target> afftargets = TargetDependencyRelation
209: .getInstance().getAffectedTargets(auxdep);
210: if (afftargets == null) {
211: return pages;
212: }
213:
214: for (Iterator<de.schlund.pfixxml.targets.Target> i = afftargets
215: .iterator(); i.hasNext();) {
216: de.schlund.pfixxml.targets.Target pfixTarget = i.next();
217: pageinfos.addAll(pfixTarget.getPageInfos());
218: }
219: for (Iterator<PageInfo> i2 = pageinfos.iterator(); i2.hasNext();) {
220: PageInfo pageinfo = i2.next();
221: Project project = projectfactory
222: .getProjectByPustefixTargetGenerator(pageinfo
223: .getTargetGenerator());
224: Variant variant = null;
225: if (pageinfo.getVariant() != null) {
226: variant = variantfactory.getVariant(pageinfo
227: .getVariant());
228: }
229: Page page = project.getPage(pageinfo.getName(), variant);
230: pages.add(page);
231: }
232: return pages;
233: }
234:
235: /*
236: * (non-Javadoc)
237: *
238: * @see de.schlund.pfixcore.editor2.core.dom.Target#getProject()
239: */
240: public Project getProject() {
241: String name = this .getName();
242: if (name.indexOf("/") == -1) {
243: // Do special handling
244: return null;
245: } else {
246: String prjName = name.substring(0, name.indexOf("/"));
247: return this .projectfactory.getProjectByName(prjName);
248: }
249: }
250:
251: public Map<String, Object> getParameters() {
252: return new HashMap<String, Object>();
253: }
254:
255: public ThemeList getThemeList() {
256: return new ThemeList() {
257:
258: public List<Theme> getThemes() {
259: ArrayList<Theme> list = new ArrayList<Theme>();
260: list.add(new AbstractTheme("default") {
261: });
262: return list;
263: }
264:
265: public boolean includesTheme(Theme theme) {
266: if (theme.getName().equals("default")) {
267: return true;
268: } else {
269: return false;
270: }
271: }
272:
273: public boolean themeOverridesTheme(Theme t1, Theme t2) {
274: return false;
275: }
276:
277: };
278: }
279:
280: }
|