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
19package org.openoffice.xmerge.merger.diff;
20
21import org.w3c.dom.Node;
22
24
25import java.util.ArrayList;
26import java.util.List;
27
43public class CharacterParser {
44
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
122 currentPosition + textLen - 1, node);
124
125 nodeList_.add(nodeEntry);
126 }
127}
This is a parser to return a character array for difference purpose.
char[] getCharArray()
Returns the character array representation of the text.
List< TextNodeEntry > getNodeList()
Returns the Node pointer with the given character position.
void addNewNodeEntry(int textLen, Node node)
Adds a new Node entry.
Object start()
Move to the beginning of the sequence.
Object next()
Move to next element in the sequence.
A small class to hold the start/end character position and the Node pointer in a text Node.
This is an implementation of the Iterator interface.
This interface contains constants for StarOffice XML tags, attributes (StarCalc cell types,...
String TAG_TAB_STOP
Element tag for text:tab-stop.
Document and PluginFactory implementations for XML based formats.
Provides interfaces for converting between two Document formats, and supports a "merge" interface for...
Definition: Convert.java:19