Results 1 to 10 of 10
  1. #1
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Retriving string from private static final class

    HI

    I want to get the content of a string created from a xml parser.

    The parsere operates within this:
    Java Code:
     private static final class SaxHandler extends DefaultHandler {
    Is there a way to get it out so I can use it in other part of my application?

    I have a
    Java Code:
     System.out.println("Item " + ItemNo +" " + Name +" " + Value +" "+ Config);
    And I want to use the content of ItemNo, Name, Value and Config to populate a table.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Retriving string from private static final class

    If you want help, you should provide an SSCCE that demonstrates the problem. Just a couple classes with a member and the correct modifiers.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Retriving string from private static final class

    Java Code:
    public class Main {
    
    	protected Shell shlOneWireComponents;
    	private Table table;
    	private Text txtConfig;
    	private Text txtItemText;
    	private Text txtItemNo;
    	private Text txtItemValue;
    	public String [] XMLItem;
    
    	/**
    	 * Launch the application.
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		try {
    			Main window = new Main();
    			window.open();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * Open the window.
    	 */
    	public void open() {
    		Display display = Display.getDefault();
    		createContents();
    		shlOneWireComponents.open();
    		shlOneWireComponents.layout();
    		while (!shlOneWireComponents.isDisposed()) {
    			if (!display.readAndDispatch()) {
    				display.sleep();
    			}
    	//	display.dispose();
    		}
    	}
    	
    
    	/**
    	 * Create contents of the window.
    	 */
    	protected void createContents() {
    		shlOneWireComponents = new Shell();
    		shlOneWireComponents.setMinimumSize(new Point(450, 450));
    		shlOneWireComponents.setSize(541, 450);
    		shlOneWireComponents.setText("One Wire components editor");
    		
    		
    		Button btnSave = new Button(shlOneWireComponents, SWT.NONE);
    		btnSave.setToolTipText("Saving the entries in the textboxes below");
    		btnSave.setFont(SWTResourceManager.getFont("Arial", 12, SWT.NORMAL));
    		btnSave.addSelectionListener(new SelectionAdapter() {
    			@Override
    			public void widgetSelected(SelectionEvent e) {
    				addToTable();
    			}
    		});
    		btnSave.setBounds(84, 10, 68, 31);
    		btnSave.setText("ADD");
    		
    		Button btnOpen = new Button(shlOneWireComponents, SWT.PUSH);
    		btnOpen.setToolTipText("Open CnodeComponents.xml file");
    		btnOpen.addSelectionListener(new SelectionAdapter() {
    			@Override
    			public void widgetSelected(SelectionEvent e) {
    				openXML();
    						}
    		});
    		btnOpen.setFont(SWTResourceManager.getFont("Arial", 12, SWT.NORMAL));
    		btnOpen.setBounds(10, 10, 68, 31);
    		btnOpen.setText("Open");
    		
    				
    		table = new Table(shlOneWireComponents, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    		table.setFont(SWTResourceManager.getFont("Arial", 12, SWT.NORMAL));
    		table.setBounds(10, 193, 517, 213);
    		table.setHeaderVisible(true);
    		table.setLinesVisible(true);
    		
    		TableColumn tblclmnItemNo = new TableColumn(table, SWT.NONE);
    		tblclmnItemNo.setWidth(100);
    		tblclmnItemNo.setText("Item No");
    		
    		TableColumn tblclmnItemText = new TableColumn(table, SWT.NONE);
    		tblclmnItemText.setWidth(215);
    		tblclmnItemText.setText("Item Text");
    		
    		TableColumn tblclmnItemValue = new TableColumn(table, SWT.NONE);
    		tblclmnItemValue.setWidth(100);
    		tblclmnItemValue.setText("Item Value");
    		
    		TableColumn tblclmnItemConfig = new TableColumn(table, SWT.NONE);
    		tblclmnItemConfig.setWidth(100);
    		tblclmnItemConfig.setText("Item Config");
    		
    		
    		
    
    	}
    	
    	public void openXML(){
    									
    		try {
                // creates and returns new instance of SAX-implementation:
                SAXParserFactory factory = SAXParserFactory.newInstance();
                
                // create SAX-parser...
                SAXParser parser = factory.newSAXParser();
                // .. define our handler:
                SaxHandler handler = new SaxHandler();
                
                // and parse:
                parser.parse("CNodeComponents.xml", handler);
                
            } catch (Exception ex) {
                ex.printStackTrace(System.out);
            }
    		
    //		TableItem item1 = new TableItem(table, SWT.NONE);
    //	    item1.setText(XMLItem);
    	}
        
        /**
         * Our own implementation of SAX handler reading
         * a purchase-order data.
         */
    	  private static final class SaxHandler extends DefaultHandler {
    	        // invoked when document-parsing is started:
    		  
    		  StringBuffer accumulator = new StringBuffer(); // Accumulate parsed text
    
    	        public void startDocument() throws SAXException {
    	            System.out.println("Document processing started");
    	        }
    	        
    	        // notifies about finish of parsing:
    	        public void endDocument() throws SAXException {
    	            System.out.println("Document processing finished");
    	        }
            public void startElement(String uri, String localName, 
                    String qName, Attributes attrs) throws SAXException {
            	 accumulator.setLength(0);
                if (qName.equals("Component")) {
                } else if (qName.equals("Type")) {
                	 
                	String TypeName = attrs.getValue("Name");
                	System.out.println("Type "+ TypeName);
                	
                } else if (qName.equals("Item")) {
                	
    //            	 ItemNo value as String:
                  String ItemNo = attrs.getValue("ItemNo");
                  // name as a String:
                  String Name = attrs.getValue("Name");
                  // value as a String:
                  String Value = attrs.getValue("Value");
                  // config value as a String:
                  String Config = attrs.getValue("Config");
                 
                  String XMLItem = ItemNo + Name + Value + Config;
                  System.out.println("Item " + ItemNo +" " + Name +" " + Value +" "+ Config);
                  
                  
                            }  else {
                            	
                    throw new IllegalArgumentException("Element '" + 
                            qName + "' is not allowed here");
                            }
                
                
            }
            
            // we leave element 'qName' without any actions:
            public void endElement(String uri, String localName, String qName)
            throws SAXException {
                // do nothing;
            }
           
           }
    			
    		public void valueChanged(Text text) {
    		    if (!text.isFocusControl())
    		      return;
    
    	}
    
    }
    Like this?

    I wold like the output from( or same as) System.out.println(); to become a string array that i can add as tableItem

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Retriving string from private static final class

    If it is private, then there isn't a way to do it without hacking something nasty together with reflection.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Retriving string from private static final class

    That's not really an SSCCE. Are you just asking how to add a getter function?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Retriving string from private static final class

    @Kevin Im not sure. I want the parsed xml strings to "jump" into my table. I know how to add to the table, but not how to get the system output into a string array that I can use..

    @quad64bit Will it help if I try to use it as a public instead of private??

    Im using SAX parser. The file is quite small. Would it be better to use DOM parser?

    Im planning on adding to the opened file later on.
    Last edited by KarlNorway; 12-21-2011 at 07:10 AM.

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Retriving string from private static final class

    Sorry, I mis-read your post, private/public doesn't have anything to do with anything.

    If you want to use that specific instance elsewhere, you can do it three ways:
    1) You could pass a reference to the SaxHandler instance to another class
    2) You could create a getter for the SaxHandler that is public, then assuming the other class in your app can 'see' the current class, you could simply call that getter to get a reference to the SaxHandler
    3) You could move the SaxHandler somewhere else (like a global context) and make it public instead of private.

    Does that help?

  8. #8
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Retriving string from private static final class

    I think that the getter function is the way to go for me (for mow anyways.)

    Oh is the Sax parser able to write to the document or do then need a DOM parser?

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Retriving string from private static final class

    I'm not sure about that, I don't know enough about how the sax parser works. Maybe someone else has an idea?

  10. #10
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Retriving string from private static final class

    After reading up om DOM and SAX Parser. I think that the propper way to go on this is to use DOM parser in a couple of "helper files" in seperate class files. I was thinking one to open and sort the xml content and one to handle saving the file and what ever the user inputs to the table..

Similar Threads

  1. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  2. static final int DONE = (1 << 9) - 1; ???
    By alacn in forum New To Java
    Replies: 2
    Last Post: 07-26-2010, 05:31 PM
  3. Replies: 3
    Last Post: 03-26-2010, 07:09 AM
  4. problem in retriving string data using jsp
    By Manas Das in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-02-2009, 08:53 AM
  5. [SOLVED] is final class members are also final ?
    By haoberoi in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 03:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •