nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.wst.css.core.metamodel.util; |
| 12 | |
| 13 | import org.eclipse.core.resources.IProject; |
| 14 | import org.eclipse.core.resources.IResource; |
| 15 | import org.eclipse.core.resources.ResourcesPlugin; |
| 16 | import org.eclipse.core.runtime.IPath; |
| 17 | import org.eclipse.core.runtime.Path; |
| 18 | import org.eclipse.wst.css.core.document.ICSSDocument; |
| 19 | import org.eclipse.wst.css.core.document.ICSSModel; |
| 20 | import org.eclipse.wst.css.core.document.ICSSNode; |
| 21 | import org.eclipse.wst.css.core.metamodel.CSSProfile; |
| 22 | import org.eclipse.wst.css.core.metamodel.CSSProfileRegistry; |
| 23 | import org.eclipse.wst.sse.core.IStructuredModel; |
| 24 | import org.eclipse.wst.sse.ui.contentproperties.ContentSettingsCreator; |
| 25 | import org.eclipse.wst.sse.ui.contentproperties.IContentSettings; |
| 26 | |
| 27 | |
| 28 | public class CSSProfileFinder { |
| 29 | |
| 30 | final static private String CSS_PROFILE = "css-profile"; //$NON-NLS-1$ |
| 31 | |
| 32 | /** |
| 33 | * Constructor for CSSProfileFinder. |
| 34 | */ |
| 35 | private CSSProfileFinder() { |
| 36 | super(); |
| 37 | } |
| 38 | |
| 39 | static synchronized public CSSProfileFinder getInstance() { |
| 40 | if (fInstance == null) { |
| 41 | fInstance = new CSSProfileFinder(); |
| 42 | } |
| 43 | return fInstance; |
| 44 | } |
| 45 | |
| 46 | public CSSProfile findProfileFor(ICSSNode node) { |
| 47 | ICSSModel model = null; |
| 48 | if (node != null) { |
| 49 | ICSSDocument doc = node.getOwnerDocument(); |
| 50 | if (doc != null) { |
| 51 | model = doc.getModel(); |
| 52 | } |
| 53 | } |
| 54 | return findProfileFor(model); |
| 55 | } |
| 56 | |
| 57 | public CSSProfile findProfileFor(IStructuredModel model) { |
| 58 | String baseLocation = null; |
| 59 | if (model instanceof ICSSModel) { |
| 60 | Object modelType = ((ICSSModel) model).getStyleSheetType(); |
| 61 | if (modelType == ICSSModel.EXTERNAL) { |
| 62 | baseLocation = model.getBaseLocation(); |
| 63 | } |
| 64 | else if (modelType == ICSSModel.EMBEDDED || modelType == ICSSModel.INLINE) { |
| 65 | baseLocation = model.getBaseLocation(); // may be null |
| 66 | } |
| 67 | } |
| 68 | else if (model != null) { |
| 69 | baseLocation = model.getBaseLocation(); |
| 70 | } |
| 71 | return findProfileFor(baseLocation); |
| 72 | } |
| 73 | |
| 74 | public CSSProfile findProfileFor(String baseLocation) { |
| 75 | CSSProfileRegistry reg = CSSProfileRegistry.getInstance(); |
| 76 | CSSProfile profile = null; |
| 77 | |
| 78 | if (baseLocation != null) { |
| 79 | IContentSettings cs = ContentSettingsCreator.create(); |
| 80 | IPath path = new Path(baseLocation); |
nitind | a47d930 | 2004-12-09 02:31:02 +0000 | [diff] [blame] | 81 | IResource resource = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); |
nitind | 901f264 | 2004-12-09 02:31:58 +0000 | [diff] [blame] | 82 | if (resource == null && path.segmentCount() > 1) |
nitind | a47d930 | 2004-12-09 02:31:02 +0000 | [diff] [blame] | 83 | resource = ResourcesPlugin.getWorkspace().getRoot().getFile(path); |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 84 | if (resource != null) { |
| 85 | IProject project = resource.getProject(); |
| 86 | // at first, initialized with project settings |
| 87 | if (project != null) { |
| 88 | String profileID = cs.getProperty(project, CSS_PROFILE); |
| 89 | if (profileID != null && 0 < profileID.length()) { |
| 90 | profile = reg.getProfile(profileID); |
| 91 | } |
| 92 | } |
| 93 | // if resource settings exist, overwrite with project settings |
| 94 | String profileID = cs.getProperty(resource, CSS_PROFILE); |
| 95 | if (profileID != null && 0 < profileID.length()) { |
| 96 | profile = reg.getProfile(profileID); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return (profile != null) ? profile : reg.getDefaultProfile(); |
| 102 | } |
| 103 | |
| 104 | static private CSSProfileFinder fInstance = null; |
| 105 | } |