| /******************************************************************************* |
| * Copyright (c) 2004 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.wst.css.core.internal.formatter; |
| |
| |
| |
| import org.eclipse.jface.text.IRegion; |
| import org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy; |
| import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode; |
| import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; |
| import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; |
| import org.w3c.dom.css.CSSUnknownRule; |
| |
| |
| /** |
| * |
| */ |
| public class UnknownRuleFormatter extends DefaultCSSSourceFormatter { |
| |
| private static UnknownRuleFormatter instance; |
| |
| /** |
| * |
| */ |
| UnknownRuleFormatter() { |
| super(); |
| } |
| |
| /** |
| * |
| */ |
| protected void formatPre(ICSSNode node, StringBuffer source) { |
| CSSCleanupStrategy stgy = getCleanupStrategy(node); |
| |
| int start = ((IndexedRegion) node).getStartOffset(); |
| int end = ((IndexedRegion) node).getEndOffset(); |
| if (end > 0) { // format source |
| IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument(); |
| CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy); |
| for (int i = 0; i < regions.length; i++) { |
| if (i != 0) |
| appendSpaceBefore(node, regions[i], source); |
| source.append(decoratedPropValueRegion(regions[i], stgy)); |
| } |
| } |
| else { // generate source |
| CSSUnknownRule rule = (CSSUnknownRule) node; |
| source.append(rule.getCssText()); |
| } |
| } |
| |
| /** |
| * |
| */ |
| protected void formatPre(ICSSNode node, IRegion region, StringBuffer source) { |
| CSSCleanupStrategy stgy = getCleanupStrategy(node); |
| |
| IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument(); |
| CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy); |
| CompoundRegion[] outside = getOutsideRegions(structuredDocument, region); |
| for (int i = 0; i < regions.length; i++) { |
| if (i != 0 || needS(outside[0])) |
| appendSpaceBefore(node, regions[i], source); |
| source.append(decoratedPropValueRegion(regions[i], stgy)); |
| } |
| if (needS(outside[1]) && !isIncludesPreEnd(node, region)) |
| appendSpaceBefore(node, outside[1], source); |
| } |
| |
| /** |
| * |
| */ |
| public synchronized static UnknownRuleFormatter getInstance() { |
| if (instance == null) |
| instance = new UnknownRuleFormatter(); |
| return instance; |
| } |
| } |