@@ -274,6 +274,36 @@ public DPTXlator createTranslator(final String dptId) throws KNXException
274274 }
275275 }
276276
277+ /**
278+ * Creates a new instance of the translator for the given datapoint type ID.
279+ *
280+ * @param dptId datapoint type ID for selecting a particular kind of value translation; if the datapoint type ID
281+ * is not part of the translator of this main type, a {@link KNXFormatException} is thrown
282+ * @return the new {@link DPTXlator} instance
283+ * @throws KNXFormatException to forward all target exceptions thrown in the constructor of the translator
284+ * @throws KNXException thrown on translator class creation errors (e.g. security/access problems)
285+ */
286+ public DPTXlator createTranslator (final DptId dptId ) throws KNXException
287+ {
288+ try {
289+ return xlator .getConstructor (String .class ).newInstance (dptId .toString ());
290+ }
291+ catch (final InvocationTargetException e ) {
292+ // try to forward encapsulated target exception
293+ if (e .getTargetException () instanceof KNXFormatException )
294+ throw (KNXFormatException ) e .getTargetException ();
295+ // throw generic message
296+ throw new KNXFormatException ("failed to init translator" , dptId .toString ());
297+ }
298+ catch (final NoSuchMethodException e ) {
299+ throw new KnxRuntimeException ("interface specification error, no public constructor(String dptId)" );
300+ }
301+ catch (final Exception e ) {
302+ // for SecurityException, InstantiationException, IllegalAccessException
303+ throw new KNXException ("failed to create translator" , e );
304+ }
305+ }
306+
277307 /**
278308 * Returns the translator class used for this main type.
279309 * <p>
@@ -524,6 +554,14 @@ public static boolean hasTranslator(final int mainNumber, final String dptId)
524554 return false ;
525555 }
526556
557+ public static DPTXlator createTranslator (final DptId dpt ) throws KNXException {
558+ final MainType type = map .get (dpt .mainNumber ());
559+ if (type == null )
560+ throw new KNXException ("no DPT translator available for " + dpt );
561+
562+ return type .createTranslator (dpt );
563+ }
564+
527565 /**
528566 * Creates a DPT translator for the given datapoint type ID.
529567 * <p>
@@ -575,7 +613,7 @@ public static DPTXlator createTranslator(final int mainNumber, final int subNumb
575613 throw new KNXException ("no DPT translator available for main number " + mainNumber );
576614
577615 final boolean withSub = subNumber != 0 ;
578- final String id = withSub ? String . format ( "%d.%03d" , mainNumber , subNumber )
616+ final String id = withSub ? new DptId ( mainNumber , subNumber ). toString ( )
579617 : type .getSubTypes ().keySet ().iterator ().next ();
580618 final DPTXlator t = type .createTranslator (id );
581619 t .setAppendUnit (withSub );
0 commit comments