Package org.omegat.util
Class EncodingSniffer
java.lang.Object
org.omegat.util.EncodingSniffer
Sniffs encoding settings from HTML, XML or other content. The HTML encoding
sniffing algorithm is based on the HTML5
encoding sniffing algorithm.
Copyright (c) 2002-2025 Gargoyle Software Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
-
Method Summary
Modifier and TypeMethodDescriptionstatic CharsetExtracts an encoding from the specifiedContent-Typevalue using the IETF algorithm; if no encoding is found, this method returnsnull.static CharsetParses and returns the charset declaration at the start of a css file if any, otherwise returnsnull.static CharsetAttempts to sniff an encoding from an HTMLmetatag in the specified byte array.static CharsetSearches the specified XML content for an XML declaration and returns the encoding if found, otherwise returnsnull.static CharsetReturnsCharsetif the specified charset name is supported on this platform.
-
Method Details
-
sniffEncodingFromMetaTag
Attempts to sniff an encoding from an HTMLmetatag in the specified byte array.- Parameters:
is- the content stream to check for an HTMLmetatag- Returns:
- the encoding sniffed from the specified bytes, or
nullif the encoding could not be determined - Throws:
IOException- if an IO error occurs
-
extractEncodingFromContentType
Extracts an encoding from the specifiedContent-Typevalue using the IETF algorithm; if no encoding is found, this method returnsnull.- Parameters:
s- theContent-Typevalue to search for an encoding- Returns:
- the encoding found in the specified
Content-Typevalue, ornullif no encoding was found
-
sniffEncodingFromXmlDeclaration
Searches the specified XML content for an XML declaration and returns the encoding if found, otherwise returnsnull.- Parameters:
is- the content stream to check for the charset declaration- Returns:
- the encoding of the specified XML content, or
nullif it could not be determined - Throws:
IOException- if an IO error occurs
-
sniffEncodingFromCssDeclaration
Parses and returns the charset declaration at the start of a css file if any, otherwise returnsnull.e.g.
@charset "UTF-8"
- Parameters:
is- the input stream to parse- Returns:
- the charset declaration at the start of a css file if any,
otherwise returns
null. - Throws:
IOException- if an IO error occurs
-
toCharset
ReturnsCharsetif the specified charset name is supported on this platform.- Parameters:
charsetName- the charset name to check- Returns:
Charsetif the specified charset name is supported on this platform
-