blob: 6d6f9261e763a60329039637a4ac5162cce045cf [file] [log] [blame]
nitind958d79a2004-11-23 19:23:00 +00001/*******************************************************************************
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 *******************************************************************************/
11package org.eclipse.wst.css.core.metamodel.util;
12
13import org.eclipse.core.resources.IProject;
14import org.eclipse.core.resources.IResource;
15import org.eclipse.core.resources.ResourcesPlugin;
16import org.eclipse.core.runtime.IPath;
17import org.eclipse.core.runtime.Path;
18import org.eclipse.wst.css.core.document.ICSSDocument;
19import org.eclipse.wst.css.core.document.ICSSModel;
20import org.eclipse.wst.css.core.document.ICSSNode;
21import org.eclipse.wst.css.core.metamodel.CSSProfile;
22import org.eclipse.wst.css.core.metamodel.CSSProfileRegistry;
23import org.eclipse.wst.sse.core.IStructuredModel;
24import org.eclipse.wst.sse.ui.contentproperties.ContentSettingsCreator;
25import org.eclipse.wst.sse.ui.contentproperties.IContentSettings;
26
27
28public 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);
nitinda47d9302004-12-09 02:31:02 +000081 IResource resource = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
nitind901f2642004-12-09 02:31:58 +000082 if (resource == null && path.segmentCount() > 1)
nitinda47d9302004-12-09 02:31:02 +000083 resource = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
nitind958d79a2004-11-23 19:23:00 +000084 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}