LibreOffice Module xmerge (master)  1
CharacterParser.java
Go to the documentation of this file.
1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  * Licensed to the Apache Software Foundation (ASF) under one or more
11  * contributor license agreements. See the NOTICE file distributed
12  * with this work for additional information regarding copyright
13  * ownership. The ASF licenses this file to you under the Apache
14  * License, Version 2.0 (the "License"); you may not use this file
15  * except in compliance with the License. You may obtain a copy of
16  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package org.openoffice.xmerge.merger.diff;
20 
21 import org.w3c.dom.Node;
22 
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
43 public class CharacterParser {
44 
45  private final TextNodeIterator textNodes;
46  private int currentPosition = 0;
47  private final List<TextNodeEntry> nodeList_;
48  private char[] charArray;
49 
55  public CharacterParser(Node node) {
56  textNodes = new TextNodeIterator(node);
57  nodeList_ = new ArrayList<TextNodeEntry>();
58 
59  parseNodes();
60  }
61 
67  public List<TextNodeEntry> getNodeList() {
68  // will go through the nodeList to find the corresponding node
69  return nodeList_;
70  }
71 
77  public char[] getCharArray() {
78  return charArray;
79  }
80 
81  private void parseNodes() {
82 
83  StringBuffer strBuf = new StringBuffer();
84 
85  /* create the character array by iterate the textnode iterator */
86  Node currentNode = (Node)(textNodes.start());
87  for (;
88  currentNode != null;
89  currentNode = (Node)(textNodes.next())) {
90 
91  // add the text value into the array
92  String textValue = null;
93  String nodeName = currentNode.getNodeName();
94 
95  // TODO: Space node have a count attribute which is not handled!
96  if (currentNode.getNodeType() == Node.TEXT_NODE) {
97  textValue = currentNode.getNodeValue();
98  } else if (nodeName.equals(OfficeConstants.TAG_SPACE)) {
99  textValue = " ";
100  } else if (nodeName.equals(OfficeConstants.TAG_TAB_STOP)) {
101  textValue = "\t";
102  }
103 
104  if (textValue != null) {
105  strBuf.append(textValue);
106  addNewNodeEntry(textValue.length(), currentNode);
107  }
108  }
109 
110  charArray = strBuf.toString().toCharArray();
111  }
112 
119  private void addNewNodeEntry(int textLen, Node node) {
120 
121  TextNodeEntry nodeEntry = new TextNodeEntry(currentPosition,
122  currentPosition + textLen - 1, node);
123  currentPosition = currentPosition + textLen;
124 
125  nodeList_.add(nodeEntry);
126  }
127 }
void addNewNodeEntry(int textLen, Node node)
Adds a new.
This interface contains constants for StarOffice XML tags, attributes (StarCalc cell types...
exports com.sun.star. java
char[] getCharArray()
Returns the character array representation of the text.
CharacterParser(Node node)
Standard constructor.
String TAG_TAB_STOP
Element tag for text:tab-stop.
Object next()
Move to next element in the sequence.
A small class to hold the start/end character position and the.
This is a parser to return a character array for difference purpose.
Provides interfaces for converting between two.
Definition: Convert.java:19
Object start()
Move to the beginning of the sequence.
List< TextNodeEntry > getNodeList()
Returns the.