Ignore:
Timestamp:
08/19/08 14:12:52 (16 years ago)
Author:
ra33
Message:

Refactored the way attribute value pairs work...
Moved static methods into attributeValuePairs class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/gui/AttributeUtils.java

    r214 r235  
    2525 */
    2626public class AttributeUtils {
    27 
    28         public static final char SEPARATOR_CHAR = ':';
    29 
    30         private static final String SEPARATOR_STRING = SEPARATOR_CHAR + " ";
    3127
    3228        private static final int GET_LENGTH = "get".length();
     
    393389                                                        attributes
    394390                                                                        .append(m.getName().substring(GET_LENGTH))
    395                                                                         .append(SEPARATOR_STRING).append(ob)
    396                                                                         .append('\n');
     391                                                                        .append(AttributeValuePair.SEPARATOR_STRING)
     392                                                                        .append(ob).append('\n');
    397393                                                continue;
    398394                                        } else if (o instanceof int[]) {
     
    411407                                        // Append the attributes
    412408                                        attributes.append(m.getName().substring(GET_LENGTH))
    413                                                         .append(SEPARATOR_STRING).append(o).append('\n');
     409                                                        .append(AttributeValuePair.SEPARATOR_STRING)
     410                                                        .append(o).append('\n');
    414411                                } catch (Exception e) {
    415412                                        // TODO Auto-generated catch block
     
    455452                List<String> values = attribs.getTextList();
    456453                // if no pairs exist, we are done
    457                 if (values == null || values.size() == 0
    458                                 || (values.size() == 1 && values.get(0).length() == 0)) {
     454                if (values == null || values.size() == 0) {
    459455                        return false;
    460456                }
     
    462458                // loop through all attribute: value pairs
    463459                for (int i = 0; i < values.size(); i++) {
    464                         StringBuffer v = new StringBuffer(values.get(i));
    465 
    466                         // remove the annotation mark (if applicable)
    467                         if (v.indexOf("@") == 0)
    468                                 v = v.deleteCharAt(0);
     460                        AttributeValuePair avp = new AttributeValuePair(values.get(i));
     461
     462                        // If the first is not an attribute value pair then dont do
     463                        // attrubute merging
     464                        if (!avp.hasAttribute())
     465                                return false;
    469466
    470467                        // check if the next string is another attribute to merge or a
    471468                        // continuation
    472                         while (i < values.size() - 1) {
    473                                 StringBuffer next = new StringBuffer(values.get(i + 1));
     469                        for (; i < values.size() - 1; i++) {
     470                                AttributeValuePair nextAvp = new AttributeValuePair(values
     471                                                .get(i + 1));
    474472
    475473                                // if the next String has a colon, then it may be another
    476474                                // attribute
    477                                 if (next.indexOf("" + SEPARATOR_CHAR) >= 0) {
     475                                if (nextAvp.hasAttribute()) {
    478476                                        // if the attribute is the same as v, then it is a
    479477                                        // continuation
    480                                         if (v.indexOf(getAttribute(next.toString())) == 0) {
     478                                        if (nextAvp.getAttribute().equals(avp.getAttribute())) {
    481479                                                // strip the attribute from next
    482                                                 next = new StringBuffer(getValue(next.toString()));
     480                                                avp.appendValue(nextAvp.getValue());
     481
    483482                                                // if the attribute is not the same, then it may be a
    484483                                                // new method
    485484                                        } else {
    486                                                 // if this is indeed a method, then leave the while loop
    487                                                 // if(_SetMethods.containsKey(StripFromColon(next.toString()).toLowerCase())){
    488485                                                break;
    489                                                 // }
    490486                                        }
    491487                                }
    492488
    493                                 v.append("\n").append(next);
    494                                 i++;
     489                                // v.append("\n").append(next);
    495490                        }
    496491
    497                         if (v.length() > 0
    498                                         && !setAttribute(toSet, v.toString(), values.size() > 1)) {
    499                                 // if no other attributes have been set
    500                                 if (i == 0)
     492                        if (!setAttribute(toSet, avp, values.size() > 1)) {
     493
     494                                String stripped = avp.getAttribute();
     495                                if (stripped == null) {
     496                                        // This happens when there is an attribute at the start
     497                                        // Then a bunch of plain text
    501498                                        return false;
    502                                 // otherwise, this is a list of attributes, so continue
    503                                 else {
    504                                         String stripped = getAttribute(v.toString());
    505                                         if (stripped == null) {
    506                                                 // This happens when there is an attribute at the start
    507                                                 // Then a bunch of plain text
    508                                                 return false;
    509                                         } else if (_Ignore.contains(stripped)) {
    510                                                 return false;
    511                                         } else if (!(_SetMethods.containsKey(stripped))) {
    512                                                 // Display an error message if its not in our list of
    513                                                 // attributes to ignore when copying
    514                                                 MessageBay.warningMessage("Attribute: '"
    515                                                                 + getAttribute(v.toString())
    516                                                                 + "' does not exist.");
    517                                         } else {
    518                                                 String types = "";
    519                                                 for (Class c : _SetMethods.get(stripped)
    520                                                                 .getParameterTypes())
    521                                                         types += c.getSimpleName() + " ";
    522                                                 MessageBay.warningMessage("Wrong arguments for: '"
    523                                                                 + getAttribute(v.toString()) + "' expecting "
    524                                                                 + types.trim() + " found '"
    525                                                                 + getValue(v.toString()) + "'");
    526                                         }
     499                                } else if (_Ignore.contains(stripped)) {
     500                                        return false;
     501                                } else if (!(_SetMethods.containsKey(stripped))) {
     502                                        // Display an error message if its not in our list of
     503                                        // attributes to ignore when copying
     504                                        MessageBay.warningMessage("Attribute: '"
     505                                                        + avp.getAttribute() + "' does not exist.");
     506                                } else {
     507                                        String types = "";
     508                                        for (Class c : _SetMethods.get(stripped)
     509                                                        .getParameterTypes())
     510                                                types += c.getSimpleName() + " ";
     511                                        MessageBay.warningMessage("Wrong arguments for: '"
     512                                                        + avp.getAttribute() + "' expecting "
     513                                                        + types.trim() + " found '" + avp.getValue() + "'");
    527514                                }
    528                         } else if (v.length() == 0)
    529                                 return false;
     515                        }
    530516                }
    531517
     
    533519        }
    534520
    535         private static boolean setAttribute(Object toSet, String value,
     521        /**
     522         * Sets a single attrubute of a frame or item.
     523         *
     524         * @param toSet
     525         * @param avp
     526         * @param isAttributeList
     527         *            some properties are ignored when attribute list are injected
     528         *            into an item. These properties are ignored if this param is
     529         *            true
     530         * @return
     531         */
     532        private static boolean setAttribute(Object toSet, AttributeValuePair avp,
    536533                        boolean isAttributeList) {
     534
     535                assert (avp.hasAttribute());
     536
    537537                // separate attribute and value from string
    538                 String attribute = getAttribute(value);
    539                 // Check that an attribute was found
    540                 if (attribute == null)
    541                         return false;
    542                 attribute = attribute.toLowerCase();
    543 
    544                 value = getValue(value);
     538                String attribute = avp.getAttribute().toLowerCase();
     539
     540                String value = avp.getValue();
    545541                assert (value != null);
    546542
     
    625621
    626622        /**
    627          * Returns the part of the given string that is after the attribute value
    628          * pair separator. If that character is not there it returns empty if it is
    629          * an annotation item or the entire string if it is not.
    630          *
    631          * @param attributeValuePair
    632          *            the string to get the value from.
    633          * @return the value from the attribute value pair.
    634          */
    635         public static String getValue(String toStrip) {
    636                 assert (toStrip != null);
    637 
    638                 toStrip = toStrip.trim();
    639                 if (toStrip.length() == 0)
    640                         return "";
    641 
    642                 int ind = toStrip.lastIndexOf(SEPARATOR_CHAR);
    643                 int lineSeparator = toStrip.indexOf(Character.LINE_SEPARATOR, ind);
    644                 // If it is an annotation item return the empty string
    645                 // Annotation items can not be values only
    646                 if (ind < 0 && toStrip.charAt(0) == '@') {
    647                         return "";
    648                 }
    649                 // If its one line then our value goes to the end of the string
    650                 if (lineSeparator < 0)
    651                         lineSeparator = toStrip.length();
    652 
    653                 return toStrip.substring(ind + 1, lineSeparator).trim();
    654         }
    655 
    656         /**
    657          * Returns the part of the given string that is before the attribute value
    658          * pair separator, or null if the given String does not include the
    659          * separator.
    660          *
    661          * @param attributeValuePair
    662          *            The String to strip
    663          * @return the attribute if there is one or null if there is not
    664          */
    665         public static String getAttribute(String attributeValuePair) {
    666                 if (attributeValuePair.length() <= 1)
    667                         return null;
    668 
    669                 attributeValuePair = attributeValuePair.trim();
    670 
    671                 if (attributeValuePair.length() == 0)
    672                         return null;
    673 
    674                 int ind = attributeValuePair.indexOf(SEPARATOR_CHAR);
    675                 // If its an annotation there must be no space between the @ and colon
    676                 // and the first character after the annotation must be a letter
    677                 if (attributeValuePair.charAt(0) == '@') {
    678                         if (!Character.isLetter(attributeValuePair.charAt(1)))
    679                                 return null;
    680                         for (int i = 2; i < ind; i++) {
    681                                 if (!Character.isLetterOrDigit(attributeValuePair.charAt(i)))
    682                                         return null;
    683                         }
    684                         if (ind < 1)
    685                                 return attributeValuePair.substring(0);
    686                 } else if (ind < 1) {
    687                         return null;
    688                 }
    689 
    690                 return attributeValuePair.substring(0, ind).trim();
    691         }
    692 
    693         /**
    694623         * Replaces the current value for the text item with the new value.
    695624         *
     
    702631                assert (newValue != null);
    703632
    704                 String oldText = text.getFirstLine();
    705                 String restOfText = text.getText().substring(oldText.length());
    706                 String attribute = getAttribute(oldText);
    707 
    708                 if (attribute == null)
    709                         attribute = text.getText().trim();
    710 
    711                 text.setText(attribute + SEPARATOR_STRING + newValue + restOfText);
    712         }
    713 
    714         /**
    715          * Gets the value from an attribute value pair as a double.
    716          *
    717          * @param attributeValuePair
    718          *            the text to get the value from
    719          * @return the double value or null if there is none
    720          */
    721         public static Double getDoubleValue(String attributeValuePair) {
    722                 String value = getValue(attributeValuePair);
    723 
    724                 assert (value != null);
    725 
    726                 try {
    727                         return Double.parseDouble(value);
    728                 } catch (Exception e) {
    729                 }
    730                 return null;
    731         }
    732 
    733         public static AttributeValuePair getPair(String text) {
    734                 // TODO Make this more efficient
    735                 String attribute = AttributeUtils.getAttribute(text);
    736                 String value = AttributeUtils.getValue(text);
    737                 if (attribute == null || value == null)
    738                         return null;
    739                 return new AttributeValuePair(attribute, value);
     633                AttributeValuePair avp = new AttributeValuePair(text.getText());
     634
     635                if (avp.getAttribute() == null) {
     636                        avp.setAttribute(avp.getValue());
     637                }
     638                avp.setValue(newValue);
     639                text.setText(avp.toString());
    740640        }
    741641}
Note: See TracChangeset for help on using the changeset viewer.