platform35/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/SyncInfoReader.java
changeset 40 eb3c938c7fef
equal deleted inserted replaced
39:2a03ec4dbf31 40:eb3c938c7fef
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2000, 2006 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.core.internal.resources;
       
    12 
       
    13 import java.io.DataInputStream;
       
    14 import java.io.IOException;
       
    15 import java.util.HashSet;
       
    16 import java.util.Set;
       
    17 import org.eclipse.core.internal.utils.Messages;
       
    18 import org.eclipse.core.resources.IResourceStatus;
       
    19 import org.eclipse.core.runtime.CoreException;
       
    20 import org.eclipse.core.runtime.QualifiedName;
       
    21 import org.eclipse.osgi.util.NLS;
       
    22 
       
    23 /**
       
    24  * This class is used to read sync info from disk. Subclasses implement
       
    25  * version specific reading code.
       
    26  */
       
    27 public class SyncInfoReader {
       
    28 	protected Workspace workspace;
       
    29 	protected Synchronizer synchronizer;
       
    30 
       
    31 	public SyncInfoReader(Workspace workspace, Synchronizer synchronizer) {
       
    32 		super();
       
    33 		this.workspace = workspace;
       
    34 		this.synchronizer = synchronizer;
       
    35 	}
       
    36 
       
    37 	/**
       
    38 	 * Returns the appropriate reader for the given version.
       
    39 	 */
       
    40 	protected SyncInfoReader getReader(int formatVersion) throws IOException {
       
    41 		switch (formatVersion) {
       
    42 			case 2 :
       
    43 				return new SyncInfoReader_2(workspace, synchronizer);
       
    44 			case 3 :
       
    45 				return new SyncInfoReader_3(workspace, synchronizer);
       
    46 			default :
       
    47 				throw new IOException(NLS.bind(Messages.resources_format, new Integer(formatVersion)));
       
    48 		}
       
    49 	}
       
    50 
       
    51 	public void readPartners(DataInputStream input) throws CoreException {
       
    52 		try {
       
    53 			int size = input.readInt();
       
    54 			Set registry = new HashSet(size);
       
    55 			for (int i = 0; i < size; i++) {
       
    56 				String qualifier = input.readUTF();
       
    57 				String local = input.readUTF();
       
    58 				registry.add(new QualifiedName(qualifier, local));
       
    59 			}
       
    60 			synchronizer.setRegistry(registry);
       
    61 		} catch (IOException e) {
       
    62 			String message = NLS.bind(Messages.resources_readSync, e);
       
    63 			throw new ResourceException(new ResourceStatus(IResourceStatus.INTERNAL_ERROR, message));
       
    64 		}
       
    65 	}
       
    66 
       
    67 	public void readSyncInfo(DataInputStream input) throws IOException, CoreException {
       
    68 		// dispatch to the appropriate reader depending
       
    69 		// on the version of the file
       
    70 		int formatVersion = readVersionNumber(input);
       
    71 		SyncInfoReader reader = getReader(formatVersion);
       
    72 		reader.readSyncInfo(input);
       
    73 	}
       
    74 
       
    75 	protected static int readVersionNumber(DataInputStream input) throws IOException {
       
    76 		return input.readInt();
       
    77 	}
       
    78 }