Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9024399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:59:55+00:00 2026-06-16T05:59:55+00:00

The original reason for my Jaxb Question JaxB reference resolving was that I couldn’t

  • 0

The original reason for my Jaxb Question
JaxB reference resolving

was that I couldn’t get the same issue working with the simple-framework:

http://old.nabble.com/Two-Phase-support-for-CycleStrategy–td34802791.html

Today I got the things working with a persister call back to the same point as in my Jaxb Questions:
I get copies – not references. Again I am looking for a solution with proper references. This time for the Simple XML framework.

The example here has the base class “ModelElement” not Person as in the other question. Otherwise the problem is the same.

Again I am calling the unmarshalling twice to get all ids in PASS 1 and use the gathered results from the lookup HashMap created in PASS2.

What would be a solution to get proper references? My assumption is that adding a call back that actually lets the called function modify the unmarshalling result (see How to use an output parameter in Java? for a wrapping approach)
would do the trick (comparable to the JaxB solution I have posted in the meantime).

Persister serializer = new Persister();
ModelElementSimpleXmlImpl.lookup.clear();
serializer.read(result, xml);
System.err.println("PASS 2");
serializer.read(result, xml);

This code is from the ModelElementSimpleXmlImpl base class:
…

  protected String ref;

  /**
   * getter for xsd:string/String id
   * @return id
   */
  @org.simpleframework.xml.Attribute(name="ref",required=false)
  public String getRef() { 
    return ref; 
  }

  /**
   * setter for xsd:string/String id
   * @param pid - new value for id
   */
  @org.simpleframework.xml.Attribute(name="ref",required=false)
  public void setRef(String pRef) { 
    ref=pRef; 
  }

  private boolean debug=true;
  /**
     * show debug information
     * @param title
     * @param key
     * @param me
     * @param found
     */
    public void showDebug(String title,String key,ModelElementSimpleXmlImpl me, ModelElementSimpleXmlImpl found) {
        String deref="?";
        if (found!=null)
            deref="->"+found.getId()+"("+found.getClass().getSimpleName()+")";
        if (debug)
            System.err.println(title+": "+key+"("+me.getClass().getSimpleName()+")"+deref+" - "+this);
    }
    /**
     * keep track of the elements already seen
     */
    public static Map<String,ModelElementSimpleXmlImpl> lookup=new HashMap<String,ModelElementSimpleXmlImpl>();

  @Validate
  public void validate() {
    ModelElementSimpleXmlImpl me=this;
    String key=me.getId();
        if (key!=null) {
            showDebug("id",key,me,null);
            lookup.put(key, me);
        }
        key=me.getRef();
        if (key!=null) {
            if (lookup.containsKey(key)) {
                ModelElementSimpleXmlImpl meRef=lookup.get(key);
                showDebug("ref",key,me,meRef);
                me.setRef(null);
        me.copyFrom(meRef);
            } else {
                if (debug)
                    showDebug("ref",me.getRef(),me,null);
            }
        }
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:59:57+00:00Added an answer on June 16, 2026 at 5:59 am

    Niall Gallagher suggested:

    Should be fairly easy to do with something like the CycleStrategy. Just create MyCycleStrategy and where there is an exception with “Invalid reference” just return null and remember the reference. When you have picked up all the ids and the values then do a second pass. In the second pass assign the value to the first occurrence of either the ref or the id. Then all following references should be given the same value. This should work.

    And he is right. The following extended Cycle Strategy works as proposed:

    /**
     * Two Path Cycle Strategy
     * 
     * @author wf
     * 
     */
    public static class TwoPathCycleStrategy extends CycleStrategy {
        String id;
        String ref;
        public static boolean debug = false;
    
        /**
         * create a twoPath Cycle Strategy
         * 
         * @param id
         * @param ref
         */
        public TwoPathCycleStrategy(String id, String ref) {
            super(id, ref);
            this.id = id;
            this.ref = ref;
        }
    
        /**
         * show debug information
         * 
         * @param title
         * @param key
         * @param value
         */
        public void showDebug(String title, String key, Value value) {
            if (debug) {
                String id = "?";
                Object v = value;
                while ((v instanceof Value) && ((Value) v).isReference()) {
                    v = ((Value) v).getValue();
                }
                if (v == null) {
                    id = "null";
                } else {
                    // FIXME - adapt to your code or comment out
                    //if (v instanceof ModelElement) {
                    //  ModelElement me = (ModelElement) v;
                    //  id = me.getId();
                    //}
                }
                System.err.println(title + ":" + key + "->"
                        + v.getClass().getSimpleName() + ":"
                        + value.getType().getSimpleName() + ":" + value.isReference() + ":"
                        + id);
            }
        }
    
        public Map<String, Value> lookup = new HashMap<String, Value>();
    
        @SuppressWarnings("rawtypes")
        @Override
        public Value read(Type type, NodeMap node, Map map) throws Exception {
            Value value = null;
            Node refNode = node.get(ref);
            Node keyNode = node.get(id);
            try {
                value = super.read(type, node, map);
                if (keyNode != null) {
                    String key = keyNode.getValue();
                    if (value != null) {
                        showDebug("id", key, value);
                        lookup.put(key, value);
                    } else {
                        showDebug("id?", key, value);
                    }
                }
            } catch (CycleException ce) {
                if (ce.getMessage().contains("Invalid reference")) {
                    if (refNode != null) {
                        String key = refNode.getValue();
                        if (lookup.containsKey(key)) {
                            value = lookup.get(key);
                            showDebug("ref", key, value);
                        } else {
                            showDebug("ref?",key,null);
                        }
                    }
                } else {
                    throw ce;
                }
            }
            return value;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ORIGINAL (see UPDATED QUESTION below) I am designing a new laboratory database that tests
Original Question: i read that for RESTful websites. it is not good to use
The original words are One reason that the distinction between process and procedure may
Original Question This is the Git repository that I have at the moment, which
My question is exactly the same as the one listed here The reason I'm
Original question: Note: Below plugin pattern based on the official jQuery docs . I'm
Original Problem: I have 3 boxes each containing 200 coins, given that there is
Original Question I've read at least a dozen articles and SO threads on events
I found that if I try to include the url in the original definition
Assume, for whatever reason, that we have three inline scripts on a page. Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.