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 | *******************************************************************************/ |
david_williams | 5677702 | 2005-04-11 06:21:55 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.html.core.internal.format; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 15 | import org.eclipse.wst.css.core.internal.format.CSSSourceFormatter; |
| 16 | import org.eclipse.wst.css.core.internal.provisional.adapters.IStyleSheetAdapter; |
| 17 | import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel; |
| 18 | import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 19 | import org.eclipse.wst.html.core.internal.provisional.HTMLFormatContraints; |
| 20 | import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter; |
| 21 | import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier; |
| 22 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode; |
| 23 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 24 | |
| 25 | // nakamori_TODO: check and remove |
| 26 | |
| 27 | public class EmbeddedCSSFormatter extends HTMLFormatter { |
| 28 | |
david_williams | d36efd9 | 2005-03-29 03:39:23 +0000 | [diff] [blame] | 29 | //private IAdapterFactory factory = new CSSSourceFormatterFactory(CSSSourceFormatter.class, true); |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 30 | /** |
| 31 | */ |
| 32 | protected EmbeddedCSSFormatter() { |
| 33 | super(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | */ |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 38 | protected void formatNode(IDOMNode node, HTMLFormatContraints contraints) { |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 39 | if (node == null) |
| 40 | return; |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 41 | IDOMText text = (IDOMText) node; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 42 | |
| 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_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 56 | private String getCSSContent(IDOMNode text) { |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 57 | 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_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 76 | private ICSSModel getCSSModel(IDOMNode text) { |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 77 | 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 | } |