Android Open Source - markj Jw Edit Element Handler






From Project

Back to project page markj.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project markj listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.markjmind.mobile.api.android.xml;
/*from w ww . jav  a2s .  c o m*/
import java.util.ArrayList;
import java.util.Hashtable;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
 * start : 2013.11.16<br>
 * <br>
 * 
 * @author ???
 * @version 2013.11.16
 */
public class JwEditElementHandler extends DefaultHandler{
  StringBuilder text = new StringBuilder();
  StringBuilder xmlString= new StringBuilder();
  int dep=0;
  String depString =null;
  String lineString =null;
  boolean isStart = false;
  Hashtable<String,String> tempTag = new Hashtable<String, String>();
  Hashtable<String,Integer> nodeIndex = new Hashtable<String, Integer>();
  public static String SEP = "+";
  
  ArrayList<String> nodePath = new ArrayList<String>();
  ArrayList<JwElement> putEndString = new ArrayList<JwElement>();
  ArrayList<JwTag> tagString = new ArrayList<JwTag>();
  
  public void putEndElementString(String nodeName, int dep,String name, String tag){
    putEndString.add(new JwElement(nodeName, dep, name, tag));
  }
  
  public void putEndElementString(String nodeName, String name, String tag){
    putEndString.add(new JwElement(nodeName, -1, name, tag));
  }
  
  public void putEndElementString(String name, String tag){
    putEndString.add(new JwElement(null, 0, name, tag));
  }
  
  public String getXmlString(){
    return xmlString.toString().trim();
  }
  public String toString(){
    return xmlString.toString().trim();
  }
    @Override
    public void startElement(String uri, String localName, String qName,Attributes attributes){
      if(dep!=0){
        addLine(lineString);
        addDep(dep, depString);
      }else{
        nodePath.clear();
        nodeIndex.clear();
      }
      xmlString.append("<");
      xmlString.append(localName);
      for(int i=0;i<attributes.getLength();i++){
        String tempLocalName = attributes.getLocalName(i);
        String value = attributes.getValue(uri, tempLocalName);
        xmlString.append(" ");
        xmlString.append(tempLocalName);
        xmlString.append("=");
        xmlString.append("\""+value+"\"");
      }
      xmlString.append(">");
      
      dep++;
      nodePath.add(localName);
      
      
      //??? ?????? ??
      String nodePathStr = "";
      int index = 0;
      for(int i=0;i<nodePath.size();i++){
        if(i==0){
          nodePathStr = nodePath.get(i);
        }else{
          nodePathStr = nodePathStr+SEP+nodePath.get(i);
        }
      }
      if(nodeIndex.containsKey(nodePathStr) ){
        index = nodeIndex.get(nodePathStr)+1;
      }
      nodeIndex.put(nodePathStr, index);
    }
    
    @Override
    public void endElement(String uri, String localName, String qName){
      dep--;
      JwElement je;
      for(int i=0;i<putEndString.size();i++){
        je = putEndString.get(i);
        if(je.nodeName.equals(localName) && (je.dep==-1 || je.dep==dep)){
          putElementString(je.name, je.tag);
        }
        if(je.nodeName==null && je.dep==0 && dep==0){
          putElementString(je.name, je.tag);
        }
      }
        
      JwTag jt;
      boolean isModify = false;
      String nodePathStr = "";
      
      for(int i=0;i<nodePath.size();i++){
        if(i<nodePath.size()-1){
          if(i==0){
            nodePathStr = nodePath.get(i);
          }else{
            nodePathStr = nodePathStr+SEP+nodePath.get(i);
          }
        }
      }
      int index =0;
      if(dep>0){
        index = nodeIndex.get(nodePathStr);
      }
      //???? ??? ??
      for(int i=0;i<tagString.size();i++){
        jt = tagString.get(i);
        if(jt.nodeName.equals(localName)){
          if(jt.nodePath==null || jt.nodePath.equals(nodePathStr)){
            if(jt.index==-1 || jt.index==index){
              addLine(lineString);
                addDep(dep+1, depString);
                xmlString.append(jt.tag);
                isModify = true;
                break;
            }
          }
        }
      }
      
      if(!isModify && text.toString().trim().length()>0){
        addLine(lineString);
        addDep(dep+1, depString);
        xmlString.append(text.toString().trim());
      }
      if(tempTag.size()>0){
        String key = nodePathStr+SEP+localName;
        if(tempTag.containsKey(key)){
          addLine(lineString);
            addDep(dep+1, depString);
          String t = tempTag.get(key);
          xmlString.append(t);
        }
      }
        
      addLine(lineString);
      addDep(dep, depString);
      xmlString.append("</");
      xmlString.append(localName);
      xmlString.append(">");
      text.setLength(0);
      if(nodePath.size()>0){
        nodePath.remove(nodePath.size()-1);
      }
    }

    @Override
    public void characters(char[] ch, int start, int length)throws SAXException {
      if(text.toString().trim().length()>0){
        String nodePathStr = "";
          for(int i=0;i<nodePath.size();i++){
            if(i==0){
              nodePathStr = nodePath.get(i);
            }else{
              nodePathStr = nodePathStr+SEP+nodePath.get(i);
            }
          }
        tempTag.put(nodePathStr, text.toString().trim());
        text.setLength(0);
      }
      
      text.append(ch,start,length);
    }
    
    public void setFormat(String lineString, String depString){
      this.lineString = lineString;
      this.depString = depString;
    }
    
    public void addDep(int dep, String depString){
      if(depString!=null){
        for(int i=0;i<dep;i++){
          xmlString.append(depString);
        }
      }
    }
    
    public void addLine(String lineString){
      if(lineString!=null){
        xmlString.append(lineString);
      }
    }
    
    public void addDep(int increase){
      if(depString!=null){
        for(int i=0;i<dep+increase;i++){
          xmlString.append(depString);
        }
      }
    }
    
    public void addLine(){
      if(lineString!=null){
        xmlString.append(lineString);
      }
    }
    public String getLine(){
      return this.lineString;
    }
    public String getDep(){
      return this.depString;
    }
    
    private void putElementString(String name, String tag){
      addLine();
      addDep(1);
      String str = "";
      if(name!=null){  
        str+= "<"+name+">";
      }
      if(tag!=null){
        str+= tag;
      }
      if(name!=null){  
        str+= "</"+name+">";
      }
    xmlString.append("<"+name+">"+tag+"</"+name+">");
    }
    
    public void setTag(String nodeName, String tag){
      tagString.add(new JwTag(null, nodeName, tag,-1));
    }
    
    public void setTag(String nodePath, String nodeName, String tag){
      tagString.add(new JwTag(nodePath, nodeName, tag, -1));
    }
    
    public void setTag(String nodePath, String nodeName, String tag, int index){
      tagString.add(new JwTag(nodePath, nodeName, tag, index));
    }
    
    
    private class JwElement{
      public String nodeName;
      public int dep;
      public String name;
      public String tag;
      public JwElement(String nodeName, int dep,String name, String tag){
        this.nodeName = nodeName;
        this.dep = dep;
        this.name = name;
        this.tag = tag;
      }
    }
    
    private class JwTag{
      String nodePath;
      String nodeName;
      String tag;
      int index;
      public JwTag(String nodePath, String nodeName, String tag, int index){
        this.nodePath = nodePath;
        this.nodeName = nodeName;
        this.tag = tag;
        this.index = index;
      }
    }
}




Java Source Code List

com.markjmind.mobile.api.android.controller.JwAsyncTask.java
com.markjmind.mobile.api.android.controller.JwMapperException.java
com.markjmind.mobile.api.android.controller.JwMemberMapper.java
com.markjmind.mobile.api.android.controller.JwStringID.java
com.markjmind.mobile.api.android.controller.JwViewController.java
com.markjmind.mobile.api.android.controller.JwViewerCache.java
com.markjmind.mobile.api.android.controller.JwViewer.java
com.markjmind.mobile.api.android.controller.Jwc.java
com.markjmind.mobile.api.android.controller.OnClickListenerReceiver.java
com.markjmind.mobile.api.android.controller.ViewerXmlMapper.java
com.markjmind.mobile.api.android.exception.JwErrorCode.java
com.markjmind.mobile.api.android.exception.JwException.java
com.markjmind.mobile.api.android.json.JSONMsgString.java
com.markjmind.mobile.api.android.json.JwJOSNTreeArray.java
com.markjmind.mobile.api.android.json.JwJSONReader.java
com.markjmind.mobile.api.android.ui.JwAnimation.java
com.markjmind.mobile.api.android.ui.JwGroup.java
com.markjmind.mobile.api.android.ui.JwMenu.java
com.markjmind.mobile.api.android.ui.JwOnGroupSelect.java
com.markjmind.mobile.api.android.ui.JwTabGroup.java
com.markjmind.mobile.api.android.ui.JwTouchGroupListener.java
com.markjmind.mobile.api.android.ui.JwTouchGroup.java
com.markjmind.mobile.api.android.ui.JwTouchMotionGroupLisener.java
com.markjmind.mobile.api.android.ui.JwTouchMotionGroup.java
com.markjmind.mobile.api.android.ui.drawgraph.GraphBoard.java
com.markjmind.mobile.api.android.ui.drawgraph.GraphSkin.java
com.markjmind.mobile.api.android.ui.drawgraph.HBackLineSkin.java
com.markjmind.mobile.api.android.ui.drawgraph.LineGraphSkin.java
com.markjmind.mobile.api.android.ui.drawgraph.PieGraph.java
com.markjmind.mobile.api.android.ui.drawgraph.RectGraphSkin.java
com.markjmind.mobile.api.android.ui.drawgraph.SimpleStyle.java
com.markjmind.mobile.api.android.ui.drawgraph.TextArraySkin.java
com.markjmind.mobile.api.android.ui.drawgraph.VBackLineSkin.java
com.markjmind.mobile.api.android.ui.graph.Arc.java
com.markjmind.mobile.api.android.ui.graph.BarElement.java
com.markjmind.mobile.api.android.ui.graph.BarLayout.java
com.markjmind.mobile.api.android.ui.graph.Bar.java
com.markjmind.mobile.api.android.ui.graph.IPieSkin.java
com.markjmind.mobile.api.android.ui.graph.LineLayout.java
com.markjmind.mobile.api.android.ui.graph.MeasureLayout.java
com.markjmind.mobile.api.android.ui.graph.PieArcRoundSkin.java
com.markjmind.mobile.api.android.ui.graph.PieLayout.java
com.markjmind.mobile.api.android.ui.graph.PiePercentSkin.java
com.markjmind.mobile.api.android.ui.view.JwBaseDropDown.java
com.markjmind.mobile.api.android.ui.view.JwDropDown.java
com.markjmind.mobile.api.android.ui.view.JwSlidingLayout.java
com.markjmind.mobile.api.android.ui.view.JwWheelView.java
com.markjmind.mobile.api.android.ui.view.StickerListener.java
com.markjmind.mobile.api.android.ui.view.StickerView.java
com.markjmind.mobile.api.android.ui.view.textview.JwTextViewAutoScale.java
com.markjmind.mobile.api.android.ui.view.textview.JwTextViewFonts.java
com.markjmind.mobile.api.android.ui.view.textview.JwTextViewWrapScale.java
com.markjmind.mobile.api.android.ui.wrapper.JwAlertDialog.java
com.markjmind.mobile.api.android.ui.wrapper.JwToast.java
com.markjmind.mobile.api.android.util.JwFile.java
com.markjmind.mobile.api.android.util.JwPreferenceList.java
com.markjmind.mobile.api.android.util.JwPreference.java
com.markjmind.mobile.api.android.util.JwSound.java
com.markjmind.mobile.api.android.util.JwTextUtils.java
com.markjmind.mobile.api.android.util.PhoneInfo.java
com.markjmind.mobile.api.android.util.WebConnection.java
com.markjmind.mobile.api.android.xml.JwDomHandler.java
com.markjmind.mobile.api.android.xml.JwDomLoader.java
com.markjmind.mobile.api.android.xml.JwEditElementHandler.java
com.markjmind.mobile.api.android.xml.JwXmlLoader.java
com.markjmind.mobile.api.android.xml.JwXmlResoure.java
com.markjmind.mobile.api.hub.ArrayJDhub.java
com.markjmind.mobile.api.hub.DBhub.java
com.markjmind.mobile.api.hub.JDhub.java
com.markjmind.mobile.api.hub.Loader.java
com.markjmind.mobile.api.hub.StoreList.java
com.markjmind.mobile.api.hub.Store.java