directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Emmanuel Lecharny (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (DIRSERVER-1929) HardwareAddress fromString does not parse output of toString
Date Thu, 12 Dec 2013 12:49:08 GMT

    [ https://issues.apache.org/jira/browse/DIRSERVER-1929?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13846284#comment-13846284
] 

Emmanuel Lecharny commented on DIRSERVER-1929:
----------------------------------------------

I have no problem replacing what we have by something better. The thing is that the code you
have posted can't compile, due to some missing classes :

HardwareAddressType,
CharMatcher, Splitter (a google class)

The ater is a bit problematic, as it's part of guavac, which is a gigantic jar we probably
don't want to include in the distribution. However, as it's AL 2.0 code, we can probably extract
the needed part and include it in our code base.

All of this can be discussed.

Another option, considering how simple is the syntax, is to parse it 'by hand' instead of
using additional external code (be it a pattern matching or any other helper class)

> HardwareAddress fromString does not parse output of toString
> ------------------------------------------------------------
>
>                 Key: DIRSERVER-1929
>                 URL: https://issues.apache.org/jira/browse/DIRSERVER-1929
>             Project: Directory ApacheDS
>          Issue Type: Bug
>          Components: dhcp
>    Affects Versions: 2.0.0-M15
>            Reporter: Shevek
>
> fromString parses "1 aa:bb:cc:dd:ee:ff" but toString generates "1/aa:bb:cc:dd:ee:ff"
> Here is code which parses both native and toString values:
>     @Nonnull
>     public static HardwareAddress fromString(@Nonnull String text) {
>         int idx = text.indexOf('/');
>         HardwareAddressType hardwareAddressType = HardwareAddressType.Ethernet;
>         if (idx != -1) {
>             try {
>                 int hardwareAddressTypeCode = Integer.parseInt(text.substring(0, idx));
>                 hardwareAddressType = HardwareAddressType.forTypeCode(hardwareAddressTypeCode);
>             } catch (IllegalArgumentException e) {
>                 hardwareAddressType = HardwareAddressType.valueOf(text.substring(0, idx));
>             }
>             text = text.substring(idx + 1);
>         }
>         CharMatcher separator = CharMatcher.BREAKING_WHITESPACE.or(CharMatcher.anyOf(":-"));
>         List<String> parts = Splitter.on(separator).omitEmptyStrings().trimResults().splitToList(text);
>         byte[] out = new byte[parts.size()];
>         for (int i = 0; i < parts.size(); i++)
>             out[i] = (byte) Integer.parseInt(parts.get(i), 16);
>         if (hardwareAddressType.getAddressLength() != -1)
>             out = Arrays.copyOf(out, hardwareAddressType.getAddressLength());
>         return new HardwareAddress(hardwareAddressType.getHardwareType(), (short) out.length,
out);
>     }



--
This message was sent by Atlassian JIRA
(v6.1.4#6159)

Mime
View raw message