001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.core;
020:
021: import java.io.IOException;
022: import org.netbeans.modules.xslt.tmap.util.Util;
023: import org.openide.ErrorManager;
024: import org.openide.filesystems.FileObject;
025: import org.openide.filesystems.FileStateInvalidException;
026: import org.openide.loaders.DataObjectExistsException;
027: import org.openide.loaders.FileEntry;
028: import org.openide.loaders.MultiDataObject;
029: import org.openide.loaders.UniFileLoader;
030: import org.openide.util.NbBundle;
031:
032: /**
033: *
034: * @author Vitaly Bychkov
035: * @version 1.0
036: *
037: * Temporary use UniFileLoader until the role
038: * of secondary file becomes clear...
039: *
040: */
041: public class XSLTDataLoader extends UniFileLoader {
042: private static final long serialVersionUID = 1L;
043:
044: public static final String MIME_TYPE = "application/xslt+xml";
045: static final String PRIMARY_EXTENSION = "xsl"; // NOI18N
046: static final String PRIMARY_EXTENSION2 = "xslt"; // NOI18N
047:
048: // TODO m
049: static final String TRANSFORM_MAP_FILE = "transformmap.xml"; // NOI18N
050:
051: // TODO r | m
052: // static final String SECONDARY_EXTENSION = "N/A"; // NOI18N
053: public static final String ACTION_CONTEXT = "Loaders/" + // NOI18N
054: MIME_TYPE + "/Actions"; // NOI18N
055: static final String LOADER_NAME = "LBL_loader_name"; // NOI18N
056:
057: public XSLTDataLoader() {
058: super ("org.netbeans.modules.xslt.core.XSLTDataObject"); // NOI18N
059: }
060:
061: /** Does initialization. Initializes display name,
062: * extension list and the actions. */
063: protected void initialize() {
064: super .initialize();
065: getExtensions().addMimeType(MIME_TYPE);
066: }
067:
068: /**
069: * Lazy init name.
070: */
071: protected String defaultDisplayName() {
072: return NbBundle.getMessage(XSLTDataLoader.class, LOADER_NAME);
073: }
074:
075: /** Creates the right data object for given primary file.
076: * It is guaranteed that the provided file is realy primary file
077: * returned from the method findPrimaryFile.
078: *
079: * @param primaryFile the primary file
080: * @return the data object for this file
081: * @exception DataObjectExistsException if the primary file already has data object
082: */
083: protected MultiDataObject createMultiObject(FileObject primaryFile)
084: throws DataObjectExistsException, IOException {
085: return new XSLTDataObject(primaryFile, this );
086: }
087:
088: protected FileObject findPrimaryFile(FileObject fo) {
089: FileObject primaryFo = null;
090: String extension = fo.getExt();
091: if (extension.equals(PRIMARY_EXTENSION)
092: || extension.equals(PRIMARY_EXTENSION2)) {
093: // recognize xslt file only in context of XSLT project
094: primaryFo = isXsltProjectContext(fo) ? fo : null;
095: }
096: return primaryFo;
097: }
098:
099: private boolean isXsltProjectContext(FileObject fo) {
100: boolean isContext = false;
101: try {
102: if (!fo.getFileSystem().isDefault() && !fo.isFolder()) {
103: String extension = fo.getExt();
104: if (extension.equals(PRIMARY_EXTENSION)
105: || extension.equals(PRIMARY_EXTENSION2)) {
106: FileObject projectSource = Util
107: .getProjectSource(Util.getProject(fo));
108: isContext = projectSource != null
109: && projectSource
110: .getFileObject(TRANSFORM_MAP_FILE) != null;
111: }
112: }
113:
114: // TODO r
115: // if (isContext) {
116: // XsltMapModel xsltMapModel = XsltMapAccessor.
117: // getXsltMapModel(getTransformMapFo(fo));
118: // isContext = xsltMapModel != null && xsltMapModel.getFirstTransformationDesc(fo) != null;
119: // }
120: } catch (FileStateInvalidException ex) {
121: ErrorManager.getDefault().notify(ex);
122: }
123:
124: return isContext;
125: }
126:
127: private boolean isEqualInputFile(String inputFile,
128: FileObject xmlFile) {
129: return inputFile != null && xmlFile != null
130: && inputFile.equals(xmlFile.getNameExt());
131: }
132:
133: private FileObject getTransformMapFo(FileObject xsltFo) {
134: return xsltFo.getParent().getFileObject(TRANSFORM_MAP_FILE);
135: }
136:
137: protected MultiDataObject.Entry createPrimaryEntry(
138: MultiDataObject obj, FileObject primaryFile) {
139: // return new XMLDataLoader.XMLFileEntry (obj, primaryFile); //adds smart templating
140: return new FileEntry(obj, primaryFile);
141: }
142:
143: /**
144: * other modules can decorate with Special Actions
145: * to see the default actions look in the layer.xml
146: */
147: protected String actionsContext() {
148: return ACTION_CONTEXT;
149: }
150: }
|