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.internal.encoding; |
| 12 | |
| 13 | |
| 14 | import org.eclipse.jface.text.IDocumentPartitioner; |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 15 | import org.eclipse.wst.css.core.internal.contenttype.EncodingGuesser; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 16 | import org.eclipse.wst.css.core.internal.parser.CSSSourceParser; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 17 | import org.eclipse.wst.css.core.internal.provisional.contenttype.ContentTypeIdForCSS; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 18 | import org.eclipse.wst.css.core.internal.text.CSSStructuredDocumentReParser; |
pavery | f918eb2 | 2005-03-29 18:26:53 +0000 | [diff] [blame] | 19 | import org.eclipse.wst.css.core.internal.text.StructuredTextPartitionerForCSS; |
david_williams | 7a65dc2 | 2005-04-09 02:19:50 +0000 | [diff] [blame] | 20 | import org.eclipse.wst.sse.core.internal.document.AbstractDocumentLoader; |
| 21 | import org.eclipse.wst.sse.core.internal.document.IDocumentCharsetDetector; |
| 22 | import org.eclipse.wst.sse.core.internal.document.IDocumentLoader; |
| 23 | import org.eclipse.wst.sse.core.internal.document.StructuredDocumentFactory; |
david_williams | dce4ddd | 2005-03-18 05:35:37 +0000 | [diff] [blame] | 24 | import org.eclipse.wst.sse.core.internal.encoding.ContentTypeEncodingPreferences; |
david_williams | 009a70e | 2005-04-13 05:41:58 +0000 | [diff] [blame] | 25 | import org.eclipse.wst.sse.core.internal.ltk.parser.RegionParser; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 26 | import org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument; |
| 27 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 28 | import org.eclipse.wst.sse.core.internal.text.BasicStructuredDocument; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | public class CSSDocumentLoader extends AbstractDocumentLoader { |
david_williams | 282b8f4 | 2005-02-14 07:00:56 +0000 | [diff] [blame] | 32 | private final static String CSS_ID = ContentTypeIdForCSS.ContentTypeID_CSS; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 33 | private IDocumentCharsetDetector documentEncodingDetector; |
| 34 | |
| 35 | |
| 36 | public CSSDocumentLoader() { |
| 37 | super(); |
| 38 | } |
| 39 | |
| 40 | protected String getEncodingNameByGuess(byte[] string, int length) { |
| 41 | String ianaEnc = null; |
| 42 | ianaEnc = EncodingGuesser.guessEncoding(string, length); |
| 43 | return ianaEnc; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Default encoding. For CSS there is no spec'd default. |
| 48 | */ |
| 49 | protected String getSpecDefaultEncoding() { |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | protected IEncodedDocument newEncodedDocument() { |
| 54 | // DMW: I copied this from CSSModelImple ... |
| 55 | // but am not sure its right |
| 56 | IStructuredDocument structuredDocument = StructuredDocumentFactory.getNewStructuredDocumentInstance(getParser()); |
| 57 | CSSStructuredDocumentReParser reParser = new CSSStructuredDocumentReParser(); |
| 58 | reParser.setStructuredDocument(structuredDocument); |
| 59 | ((BasicStructuredDocument) structuredDocument).setReParser(reParser); |
| 60 | return structuredDocument; |
| 61 | } |
| 62 | |
| 63 | public RegionParser getParser() { |
| 64 | // return new CSSRegionParser(); |
| 65 | return new CSSSourceParser(); |
| 66 | } |
| 67 | |
| 68 | protected String getPreferredNewLineDelimiter() { |
| 69 | return ContentTypeEncodingPreferences.getPreferredNewLineDelimiter(CSS_ID); |
| 70 | } |
| 71 | |
| 72 | public IDocumentCharsetDetector getDocumentEncodingDetector() { |
| 73 | if (documentEncodingDetector == null) { |
| 74 | documentEncodingDetector = new CSSDocumentCharsetDetector(); |
| 75 | } |
| 76 | return documentEncodingDetector; |
| 77 | } |
| 78 | |
| 79 | public IDocumentPartitioner getDefaultDocumentPartitioner() { |
| 80 | return new StructuredTextPartitionerForCSS(); |
| 81 | } |
| 82 | |
| 83 | public IDocumentLoader newInstance() { |
| 84 | return new CSSDocumentLoader(); |
| 85 | } |
| 86 | |
| 87 | } |