blob: 294906cb5952ddb7b5ddfd800fea6b9656cc6a92 [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 *******************************************************************************/
david_williams56777022005-04-11 06:21:55 +000011package org.eclipse.wst.html.core.internal.format;
nitind958d79a2004-11-23 19:23:00 +000012
13
14
david_williams63219a22005-04-10 01:59:51 +000015import org.eclipse.wst.css.core.internal.format.CSSSourceFormatter;
16import org.eclipse.wst.css.core.internal.provisional.adapters.IStyleSheetAdapter;
17import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel;
18import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
david_williams4ad020f2005-04-18 08:00:30 +000019import org.eclipse.wst.html.core.internal.provisional.HTMLFormatContraints;
20import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
21import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
22import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
23import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText;
nitind958d79a2004-11-23 19:23:00 +000024
25// nakamori_TODO: check and remove
26
27public class EmbeddedCSSFormatter extends HTMLFormatter {
28
david_williamsd36efd92005-03-29 03:39:23 +000029 //private IAdapterFactory factory = new CSSSourceFormatterFactory(CSSSourceFormatter.class, true);
nitind958d79a2004-11-23 19:23:00 +000030 /**
31 */
32 protected EmbeddedCSSFormatter() {
33 super();
34 }
35
36 /**
37 */
david_williamsc39caaf2005-04-05 06:07:16 +000038 protected void formatNode(IDOMNode node, HTMLFormatContraints contraints) {
nitind958d79a2004-11-23 19:23:00 +000039 if (node == null)
40 return;
david_williamsc39caaf2005-04-05 06:07:16 +000041 IDOMText text = (IDOMText) node;
nitind958d79a2004-11-23 19:23:00 +000042
43 String source = getCSSContent(node);
44 if (source == null) { // fallback
45 source = text.getSource();
46 }
47
48 int offset = text.getStartOffset();
49 int length = text.getEndOffset() - offset;
50 replaceSource(text.getModel(), offset, length, source);
51 setWidth(contraints, source);
52 }
53
54 /**
55 */
david_williamsc39caaf2005-04-05 06:07:16 +000056 private String getCSSContent(IDOMNode text) {
nitind958d79a2004-11-23 19:23:00 +000057 ICSSModel model = getCSSModel(text);
58 if (model == null)
59 return null;
60 ICSSNode document = model.getDocument();
61 if (document == null)
62 return null;
63 INodeNotifier notifier = (INodeNotifier) document;
64 INodeAdapter adapter = notifier.getAdapterFor(CSSSourceFormatter.class);
65 if (adapter == null)
66 return null;
67 CSSSourceFormatter formatter = (CSSSourceFormatter) adapter;
68 StringBuffer buffer = formatter.format(document);
69 if (buffer == null)
70 return null;
71 return buffer.toString();
72 }
73
74 /**
75 */
david_williamsc39caaf2005-04-05 06:07:16 +000076 private ICSSModel getCSSModel(IDOMNode text) {
nitind958d79a2004-11-23 19:23:00 +000077 if (text == null)
78 return null;
79 INodeNotifier notifier = (INodeNotifier) text.getParentNode();
80 if (notifier == null)
81 return null;
82 INodeAdapter adapter = notifier.getAdapterFor(IStyleSheetAdapter.class);
83 if (adapter == null)
84 return null;
85 if (!(adapter instanceof IStyleSheetAdapter))
86 return null;
87 IStyleSheetAdapter styleAdapter = (IStyleSheetAdapter) adapter;
88 return styleAdapter.getModel();
89 }
90}