/************************************************************************** * EntityPath.java * * $Revision: 1.14 $ * $Date: 2006/03/03 13:52:21 $ * * Copyright (C) 2001 CERN - European Organization for Nuclear Research * All rights reserved. **************************************************************************/ package com.c2kernel.lookup; import java.util.ArrayList; import com.c2kernel.common.ObjectNotFoundException; /** * Extends Path to enforce SystemKey structure and support int form * * @version $Revision: 1.14 $ $Date: 2006/03/03 13:52:21 $ * @author $Author: abranson $ **/ public class ItemPath extends Path { // no of components in a syskey public static final int elementNo = 3; // no of digits in a syskey component public static final int elementLen = 3; // maximum possible int syskey public static final int maxSysKey = (int)Math.pow(10, elementNo*elementLen)-1; protected static String mTypeRoot; /* * From syskey int * Note no EntityPath constructors allow setting of CONTEXT or ENTITY: * The object decides that for itself from the number of components */ public ItemPath(int syskey) throws InvalidItemPathException { super(); setSysKey(syskey); } /* */ public ItemPath() { super(); } /* */ public ItemPath(String[] path) throws InvalidItemPathException { super(path, Path.CONTEXT); // dummy - it will get replaced in checkSysPath() checkSysPath(); } /* */ public ItemPath(String path) throws InvalidItemPathException { super(path, Path.CONTEXT); checkSysPath(); } /* */ public ItemPath(ItemPath parent, String child) throws InvalidItemPathException { super(parent, child); checkSysPath(); } // EntityPaths root in /entity @Override public String getRoot() { return "entity"; } @Override public ItemPath getEntity() throws ObjectNotFoundException { return this; } public byte[] getOID() { if (mSysKey == Path.INVALID) return null; return String.valueOf(mSysKey).getBytes(); } /*************************************************************************/ /** Returns int form of syskey (if possible) */ @Override public int getSysKey() { if (mSysKey == Path.INVALID && mType == Path.ENTITY) try { if (mPath.length != elementNo) throw new InvalidItemPathException("Incorrect number of components for a system key"); mSysKey = 0; for (String keypart : mPath) { if (keypart.length()!=elementLen+1) throw new InvalidItemPathException("Component '"+keypart+"' is not the correct size for a system key"); for(int j=1; j= '0') && (c <='9')) mSysKey = mSysKey*10 + c - '0'; else throw new InvalidItemPathException("Component '"+keypart+"' contained a non-numeric char (excluding first char 'd')."); } } } catch (InvalidItemPathException ex) { mSysKey = Path.INVALID; } return mSysKey; } /** Sets path from int syskey */ public void setSysKey(int sysKey) throws InvalidItemPathException { if (sysKey < 0 || sysKey > maxSysKey) throw new InvalidItemPathException("System key "+sysKey+" out of range"); String stringPath = Integer.toString(sysKey); ArrayList newKey = new ArrayList(); for (int i=0; i elementNo) throw new InvalidItemPathException("EntityPath cannot have more than "+elementNo+" components: "+toString()); if (mPath.length == elementNo) mType = Path.ENTITY; else mType = Path.CONTEXT; } }