styletextpropatom.java

来自「EXCEL read and write」· Java 代码 · 共 481 行 · 第 1/2 页

JAVA
481
字号
	public long getRecordType() { return _type; }	/**	 * Write the contents of the record back, so it can be written	 *  to disk	 */	public void writeOut(OutputStream out) throws IOException {		// First thing to do is update the raw bytes of the contents, based		//  on the properties		updateRawContents();		// Now ensure that the header size is correct		int newSize = rawContents.length + reserved.length;		LittleEndian.putInt(_header,4,newSize);		// Write out the (new) header		out.write(_header);		// Write out the styles		out.write(rawContents);		// Write out any extra bits		out.write(reserved);	}	/**	 * Tell us how much text the parent TextCharsAtom or TextBytesAtom	 *  contains, so we can go ahead and initialise ourselves.	 */	public void setParentTextSize(int size) {		int pos = 0;		int textHandled = 0;		// While we have text in need of paragraph stylings, go ahead and		// grok the contents as paragraph formatting data        int prsize = size;		while(pos < rawContents.length && textHandled < prsize) {			// First up, fetch the number of characters this applies to			int textLen = LittleEndian.getInt(rawContents,pos);			textHandled += textLen;			pos += 4;			short indent = LittleEndian.getShort(rawContents,pos);			pos += 2;			// Grab the 4 byte value that tells us what properties follow			int paraFlags = LittleEndian.getInt(rawContents,pos);			pos += 4;			// Now make sense of those properties			TextPropCollection thisCollection = new TextPropCollection(textLen, indent);			int plSize = thisCollection.buildTextPropList(					paraFlags, paragraphTextPropTypes, rawContents, pos);			pos += plSize;			// Save this properties set			paragraphStyles.add(thisCollection);            // Handle extra 1 paragraph styles at the end            if(pos < rawContents.length && textHandled == size) {                prsize++;            }		}        if (rawContents.length > 0 && textHandled != (size+1)){            logger.log(POILogger.WARN, "Problem reading paragraph style runs: textHandled = " + textHandled + ", text.size+1 = " + (size+1));        }		// Now do the character stylings		textHandled = 0;        int chsize = size;		while(pos < rawContents.length && textHandled < chsize) {			// First up, fetch the number of characters this applies to			int textLen = LittleEndian.getInt(rawContents,pos);			textHandled += textLen;			pos += 4;			// There is no 2 byte value			short no_val = -1;			// Grab the 4 byte value that tells us what properties follow			int charFlags = LittleEndian.getInt(rawContents,pos);			pos += 4;			// Now make sense of those properties			// (Assuming we actually have some)			TextPropCollection thisCollection = new TextPropCollection(textLen, no_val);			int chSize = thisCollection.buildTextPropList(					charFlags, characterTextPropTypes, rawContents, pos);			pos += chSize;			// Save this properties set			charStyles.add(thisCollection);						// Handle extra 1 char styles at the end			if(pos < rawContents.length && textHandled == size) {				chsize++;			}		}        if (rawContents.length > 0 && textHandled != (size+1)){            logger.log(POILogger.WARN, "Problem reading character style runs: textHandled = " + textHandled + ", text.size+1 = " + (size+1));        }		// Handle anything left over		if(pos < rawContents.length) {			reserved = new byte[rawContents.length-pos];			System.arraycopy(rawContents,pos,reserved,0,reserved.length);		}		initialised = true;	}	/**	 * Updates the cache of the raw contents. Serialised the styles out.	 */	private void updateRawContents() throws IOException {		if(!initialised) {			// We haven't groked the styles since creation, so just stick			// with what we found			return;		}		ByteArrayOutputStream baos = new ByteArrayOutputStream();		// First up, we need to serialise the paragraph properties		for(int i=0; i<paragraphStyles.size(); i++) {			TextPropCollection tpc = (TextPropCollection)paragraphStyles.get(i);			tpc.writeOut(baos);		}		// Now, we do the character ones		for(int i=0; i<charStyles.size(); i++) {			TextPropCollection tpc = (TextPropCollection)charStyles.get(i);			tpc.writeOut(baos);		}		rawContents	= baos.toByteArray();	}    public void setRawContents(byte[] bytes) {        rawContents = bytes;        reserved = new byte[0];        initialised = false;    }    /**	 * Create a new Paragraph TextPropCollection, and add it to the list	 * @param charactersCovered The number of characters this TextPropCollection will cover	 * @return the new TextPropCollection, which will then be in the list	 */	public TextPropCollection addParagraphTextPropCollection(int charactersCovered) {		TextPropCollection tpc = new TextPropCollection(charactersCovered, (short)0);		paragraphStyles.add(tpc);		return tpc;	}	/**	 * Create a new Character TextPropCollection, and add it to the list	 * @param charactersCovered The number of characters this TextPropCollection will cover	 * @return the new TextPropCollection, which will then be in the list	 */	public TextPropCollection addCharacterTextPropCollection(int charactersCovered) {		TextPropCollection tpc = new TextPropCollection(charactersCovered);		charStyles.add(tpc);		return tpc;	}	/* ************************************************************************ */	/**     * Dump the record content into <code>StringBuffer</code>     *     * @return the string representation of the record data     */    public String toString(){        StringBuffer out = new StringBuffer();        	    out.append("StyleTextPropAtom:\n");        if (!initialised) {	        out.append("Uninitialised, dumping Raw Style Data\n");        } else {        	        out.append("Paragraph properties\n");	        	        for (Iterator it1 = getParagraphStyles().iterator(); it1.hasNext();) {	            TextPropCollection pr = (TextPropCollection)it1.next();	            out.append("  chars covered: " + pr.getCharactersCovered());	            out.append("  special mask flags: 0x" + HexDump.toHex(pr.getSpecialMask()) + "\n");	            for (Iterator it2 = pr.getTextPropList().iterator(); it2.hasNext(); ) {	                TextProp p = (TextProp)it2.next();	                out.append("    " + p.getName() + " = " + p.getValue() );	                out.append(" (0x" + HexDump.toHex(p.getValue()) + ")\n");	            }	            	            out.append("  para bytes that would be written: \n");	            	            try {					ByteArrayOutputStream baos = new ByteArrayOutputStream();					pr.writeOut(baos);					byte[] b = baos.toByteArray();					out.append(HexDump.dump(b, 0, 0));	            } catch (Exception e ) {	            	e.printStackTrace();	            }	        }		        out.append("Character properties\n");	        for (Iterator it1 = getCharacterStyles().iterator(); it1.hasNext();) {	            TextPropCollection pr = (TextPropCollection)it1.next();	            out.append("  chars covered: " + pr.getCharactersCovered() );	            out.append("  special mask flags: 0x" + HexDump.toHex(pr.getSpecialMask()) + "\n");	            for (Iterator it2 = pr.getTextPropList().iterator(); it2.hasNext(); ) {	                TextProp p = (TextProp)it2.next();	                out.append("    " + p.getName() + " = " + p.getValue() );	                out.append(" (0x" + HexDump.toHex(p.getValue()) + ")\n");	            }	            	            out.append("  char bytes that would be written: \n");	            	            try {					ByteArrayOutputStream baos = new ByteArrayOutputStream();					pr.writeOut(baos);					byte[] b = baos.toByteArray();					out.append(HexDump.dump(b, 0, 0));	            } catch (Exception e ) {	            	e.printStackTrace();	            }	        }        }        	        out.append("  original byte stream \n");		out.append( HexDump.dump(rawContents, 0, 0) );        return out.toString();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?