All pastes #756837 Raw Edit

Jemand

public java v1 · immutable
#756837 ·published 2007-10-31 19:43 UTC
rendered paste body
import java.io.*;/** * The Class XMLEncoding. *  * finds the coding type of xml-files */public class XMLEncoding {		private final String[] UTF = { "3C", "0" };	/** The file. */	private File file = null;	/**	 * Instantiates a new XMLEncoding.	 * 	 * @param file the file which has to be checked	 */		public XMLEncoding(File file) {		this.file = file;	}		/**	 * Encode.	 * 	 * @return string with a comment and the coding of the xml-file	 */		public String encode() {		String coding = "Das Dokument ist codiert in: ";		coding += testDeclaration();		return coding;	}	/**	 * Test declaration.	 * 	 * @return string with the coding of the xml-file	 */	private String testDeclaration() {		String coding = null;		try {			LineNumberReader reader = new LineNumberReader(new FileReader(file));			coding = reader.readLine();		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}		int start = coding.lastIndexOf("encoding=\"");		if (start == -1) {			return testBytes(coding);		} else {			int end = start;			while (coding.charAt(end) != '"') {				end++;			}			coding = coding.substring(start, end);		}		return coding;	}	/**	 * Test bytes.	 * 	 * @param coding	 * 	 * @return string with the coding of the xml-file  	 * 	 */		private String testBytes(String coding) {		int charFormat = -1;		byte[] b = coding.getBytes();		if (String.format("%X", b[0]).equals(UTF[0])				&& String.format("%X", b[1]).equals(UTF[1])) {			charFormat = 0;		} else if (String.format("%X", b[0]).equals(UTF[1])				&& String.format("%X", b[1]).equals(UTF[0])) {			charFormat = 1;		}		switch (charFormat) {		case -1:			coding = "UTF 8";			break;		case 0:			coding = "UTF 16 LE";			break;		case 1:			coding = "UTF 16 BE";			break;		}		return coding;	}	/**	 * The main method.	 * 	 * @param args 	 */	public static void main(String args[]) {		if (args.length == 0) {			System.err.println("Bitte Dateipfad als Parameter übergeben");		} else {			XMLEncoding encoder = new XMLEncoding(new File(args[0]));			System.out.println(encoder.encode());		}	}}